Chapters 6 and 7 Line-Based File Input, Arrays reading: , 7.1

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Line-based file processing reading: 6.3 self-check: #7-11 exercises: #1-4, 8-11.
Advertisements

Files 1. 2 Storage Modern computers have many different kinds of storage components: - memory (aka RAM or DRAM) - disks (aka “hard disks” or “hard drives”)
Copyright 2008 by Pearson Education Building Java Programs Chapter 6 Lecture 6-1: File Input with Scanner reading: , 5.3 self-check: Ch. 6 #1-6.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 6: File Processing.
1 CSE 142 Lecture Notes File input using Scanner Suggested reading: , Suggested self-checks: Section 6.7 # 1-11, These lecture.
1 Files Readings: 6.1 – Reading data from files Creating a Scanner for a file, general syntax: Scanner = new Scanner(new File(" ")); Example: Scanner.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 6: File Processing.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 6: File Processing.
Topic 20 more file processing Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 4.3, 7.6.
CS305j Introduction to Computing File Processing 1 Topic 18 File Processing " We have also obtained a glimpse of another crucial idea about languages and.
Copyright 2010 by Pearson Education Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
1 Hours question Given a file hours.txt with the following contents: 123 Kim Eric Stef
BUILDING JAVA PROGRAMS CHAPTER 6 File Processing.
1 BUILDING JAVA PROGRAMS CHAPTER 7 LECTURE 7-3: ARRAYS AS PARAMETERS; FILE OUTPUT.
Topic 19 file input, line based Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops; Procedural Design reading: 5.1 – 5.2; 4.5.
1 BUILDING JAVA PROGRAMS CHAPTER 6 DETAILS OF TOKEN-BASED PROCESSING.
Week 7 Review and file processing. >>> Methods Hello.java public class Hello { public static void main(String[] args){ hello(); } public static void hello(){
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays as Parameters; File Output reading: 7.1, 4.3, 3.3 self-checks:
Copyright 2008 by Pearson Education Building Java Programs Chapter 6: File Processing Lecture 6-2: Advanced file input reading: self-check: #7-11.
1 Building Java Programs Chapter 6 Lecture 6-3: Section Problems reading:
FILE PROCESSING. Reading files To read a file, pass a File when constructing a Scanner. Scanner name = new Scanner(new File(" file name ")); Example:
Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
1 Reminder: String Scanners A Scanner can be constructed to tokenize a particular String, such as one line of an input file. Scanner = new Scanner( );
File Processing Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from "We can only.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 17: Arrays for Tallying; Text Processing reading: 4.3, 7.6 (Slides adapted.
1 Line-based file processing suggested reading:6.3.
Copyright 2008 by Pearson Education Building Java Programs Chapter 6 Lecture 6-3: Searching Files reading: 6.3, 6.5.
1 Line-based file processing reading: 6.3 Use this if you seem to need line based processing for some aspects of your program and token based for others.
Chapter 2 Clarifications
Building Java Programs
CSc 110, Autumn 2017 Lecture 20: Line-Based File Input
CSc 110, Spring 2017 Lecture 18: Line-Based File Input
CSc 110, Autumn 2017 Lecture 21: Line-Based File Input
Line-based file processing
Building Java Programs
Topic 20 more file processing
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
CSc 110, Spring 2017 Lecture 17: Line-Based File Input
Lecture 7-3: Arrays for Tallying; Text Processing
Building Java Programs
CSc 110, Autumn 2016 Lecture 17: Line-Based File Input
CSc 110, Spring 2018 Lecture 22: Line-Based File Input
Building Java Programs
Building Java Programs
CSc 110, Spring 2018 Lecture 21: Line-Based File Input
CSc 110, Autumn 2016 Programming feel like that?
Input/output (I/O) import java.io.*;
Building Java Programs
Building Java Programs
Building Java Programs
CSC1401 Input and Output (with Files)
Building Java Programs
Building Java Programs
Files Readings: 6.1 – 6.2.
Building Java Programs
Building Java Programs
Topic 19 file input, line based
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

Chapters 6 and 7 Line-Based File Input, Arrays reading: 6.3 - 6.5, 7.1 CSE 142, Spring 2013 Chapters 6 and 7 Line-Based File Input, Arrays reading: 6.3 - 6.5, 7.1

Programming feel like that?

IMDb movies problem Consider the following Internet Movie Database (IMDb) data: 1 9.1 196376 The Shawshank Redemption (1994) 2 9.0 139085 The Godfather: Part II (1974) 3 8.8 81507 Casablanca (1942) Write a program that displays any movies containing a phrase: Search word? part Rank Votes Rating Title 2 139085 9.0 The Godfather: Part II (1974) 40 129172 8.5 The Departed (2006) 95 20401 8.2 The Apartment (1960) 192 30587 8.0 Spartacus (1960) 4 matches. Is this a token or line-based problem?

"Chaining" main should be a concise summary of your program. It is bad if each method calls the next without ever returning (we call this chaining): A better structure has main make most of the calls. Methods must return values to main to be passed on later. main methodA methodB methodC methodD main methodA methodB methodC methodD

Bad IMDb "chained" code 1 // Displays IMDB's Top 250 movies that match a search string. import java.io.*; // for File import java.util.*; // for Scanner public class Movies { public static void main(String[] args) throws FileNotFoundException { getWord(); } // Asks the user for their search word and returns it. public static void getWord() throws FileNotFoundException { System.out.print("Search word: "); Scanner console = new Scanner(System.in); String searchWord = console.next(); searchWord = searchWord.toLowerCase(); System.out.println(); Scanner input = new Scanner(new File("imdb.txt")); search(input, searchWord); ...

Bad IMDb "chained" code 2 ... // Breaks apart each line, looking for lines that match the search word. public static String search(Scanner input, String searchWord) { int matches = 0; while (input.hasNextLine()) { String line = input.nextLine(); String lineLC = line.toLowerCase(); // case-insensitive match if (lineLC.indexOf(searchWord) >= 0) { matches++; System.out.println("Rank\tVotes\tRating\tTitle"); display(line); } System.out.println(matches + " matches."); // Displays the line in the proper format on the screen. public static void display(String line) { Scanner lineScan = new Scanner(line); int rank = lineScan.nextInt(); double rating = lineScan.nextDouble(); int votes = lineScan.nextInt(); String title = ""; while (lineScan.hasNext()) { title += lineScan.next() + " "; // the rest of the line System.out.println(rank + "\t" + votes + "\t" + rating + "\t" + title);

Better IMDb answer 1 // Displays IMDB's Top 250 movies that match a search string. import java.io.*; // for File import java.util.*; // for Scanner public class Movies { public static void main(String[] args) throws FileNotFoundException { String searchWord = getWord(); Scanner input = new Scanner(new File("imdb.txt")); String line = search(input, searchWord); if (line.length() > 0) { System.out.println("Rank\tVotes\tRating\tTitle"); while (line.length() > 0) { display(line); line = search(input, searchWord); } System.out.println(matches + " matches."); // Asks the user for their search word and returns it. public static String getWord() { System.out.print("Search word: "); Scanner console = new Scanner(System.in); String searchWord = console.next(); searchWord = searchWord.toLowerCase(); System.out.println(); return searchWord; ...

Better IMDb answer 2 ... // Breaks apart each line, looking for lines that match the search word. public static String search(Scanner input, String searchWord) { while (input.hasNextLine()) { String line = input.nextLine(); String lineLC = line.toLowerCase(); // case-insensitive match if (lineLC.indexOf(searchWord) >= 0) { return line; } return ""; // not found // Displays the line in the proper format on the screen. public static void display(String line) { Scanner lineScan = new Scanner(line); int rank = lineScan.nextInt(); double rating = lineScan.nextDouble(); int votes = lineScan.nextInt(); String title = ""; while (lineScan.hasNext()) { title += lineScan.next() + " "; // the rest of the line System.out.println(rank + "\t" + votes + "\t" + rating + "\t" + title);