in front of each line. Example input: file msg.txt Chris, Can you please modify the a5/turnin settings to make CSE 142 Homework 5 due Wednesday, July 27 at 11:59pm instead of due tomorrow at 6pm? Thanks, Pat Example output: >Chris, > > Can you please modify the a5/turnin settings > to make CSE 142 Homework 5 due Wednesday, > July 27 at 11:59pm instead of due tomorrow > at 6pm? > > Thanks, Pat"> in front of each line. Example input: file msg.txt Chris, Can you please modify the a5/turnin settings to make CSE 142 Homework 5 due Wednesday, July 27 at 11:59pm instead of due tomorrow at 6pm? Thanks, Pat Example output: >Chris, > > Can you please modify the a5/turnin settings > to make CSE 142 Homework 5 due Wednesday, > July 27 at 11:59pm instead of due tomorrow > at 6pm? > > Thanks, Pat">
Download presentation
Presentation is loading. Please wait.
Published byCorey Berry Modified over 8 years ago
1
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. Line based can do both. Token-based can’t.
2
Line-based Processing (Section 6.3) Line-based Scanner s Scanner input = new Scanner(new File(" file name ")); while (input.hasNextLine()) { String line = input.nextLine(); process this line ; // scan this line using a String // scanner (see Scanners on // Strings, below). } MethodDescription nextLine() returns next entire line of input (from cursor to \n ) This is the only non-token-based Scanner method. Also consumes the terminating \n. hasNextLine() returns true if there are any more lines of input to read (always true for console input) False when EOF (end of file) is only thing left.
3
3 File processing question Write a program that reads a text file and "quotes" it by putting a > in front of each line. Example input: file msg.txt Chris, Can you please modify the a5/turnin settings to make CSE 142 Homework 5 due Wednesday, July 27 at 11:59pm instead of due tomorrow at 6pm? Thanks, Pat Example output: >Chris, > > Can you please modify the a5/turnin settings > to make CSE 142 Homework 5 due Wednesday, > July 27 at 11:59pm instead of due tomorrow > at 6pm? > > Thanks, Pat
4
4 File processing answer import java.io.*; // for File import java.util.*; // for Scanner public class QuoteMessage { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("msg.txt")); while (input.hasNextLine()) { String line = input.nextLine(); System.out.println(">" + line); }
5
Consuming lines of input 23 3.14 John Smith "Hello" world 45.219 47 The Scanner consumes a line through the \n but returns everything except the \n. This is why it is called line processing. Examples: (The portion of the file returned is bold.) 23\t3.14 John Smith\t"Hello" world\n\t\t45.2 19\n 47 ^ – String line = input.nextLine(); 23\t3.14 John Smith\t"Hello" world\n\t\t45.2 19\n 47 ^ line now contains 23\t3.14 John Smith\t"Hello" world – String line2 = input.nextLine(); 23\t3.14 John Smith\t"Hello" world\n\t\t45.2 19\n 47 ^ line2 now contains \t\t45.2 19 – Each \n character is consumed but not returned as part of the string.
6
Scanners on Strings A Scanner can tokenize each line ( String) in a file: Scanner name = new Scanner( String ); – Example: String text = "15 3.2 hello 9 27.5"; Scanner scan = new Scanner(text); int num = scan.nextInt(); System.out.println(num); // 15 double num2 = scan.nextDouble(); System.out.println(num2); // 3.2 String word = scan.next(); System.out.println(word); // hello As in the scanner from the console the scanner ignores all white space – spaces, new lines and tabs. if reading the line of text from the console, there will never be a \n in one of these strings because the input.nextLine() method never returns a \n. input.nextLine() reads the next line and returns the string up to the \n, then it consumes the \n.
7
7 Advantage of using line processing with a string scanner. The line processing brings in a line, stopping at the \n. Token processing reads in tokens, ignoring \n’s. The end of the line is often an important marker that token processing ignores but line processing retains.
8
8 Methods of Scanner for a string are the same as we have seen for file and console input: reads and returns a double value nextDouble() reads and returns an int value nextInt() reads and returns the next token* as a String next() DescriptionMethod * A token on input is any contiguous data separated by white space. We will see examples later. hasNext and hasNextLine can each return false. Console could not. Method NameDescription hasNext() whether any more tokens remain hasNextDouble() whether the next token can be interpreted as type double hasNextInt() whether the next token can be interpreted as type int
9
Breaking input lines into Strings, then tokens. // Counts the words on each line of a file int lineCount = 0; Scanner input = new Scanner(new File("input.txt")); while (input.hasNextLine()) { String line = input.nextLine(); // works with a line Scanner lineScan = new Scanner(line); lineCount++; // process the contents of this line int count = 0; while (lineScan.hasNext()) { String word = lineScan.next(); // works with token count++; } System.out.println("Line " + lineCount + " contains " + count + " words"); } } Input file input.txt : Output to console: The quick brown fox jumps over the lazy dog. Line 1 contains 6 words Line 2 contains 3 words Note: The last word is “dog.”.
10
Hours question A file (hours.txt) contains the employees ID number, their name (first name only) and the number of hours they worked on each day they worked. Print the total number of hours worked and the average hours worked per day. Note, different employees work a different number of days. 123 Kim 12.5 8.1 7.6 3.2 456 Eric 4.0 11.6 6.5 2.7 12 789 Stef 8.0 8.0 8.0 8.0 7.5 – It should produce the following output: Kim (ID# 123) worked 31.4 hours (7.9 hours/day) Eric (ID# 456) worked 36.8 hours (7.4 hours/day) Stef (ID# 789) worked 39.5 hours (7.9 hours/day) With token-based processing we would not know where the end of line is. For example, we would read Kim’s ID, name and the 4 time amounts she worked. But, the 456 (Eric’s ID) would probably be read as hours because the file.nextDouble() would not see the \n. Kim would be given an extra 456 hours! But the program would then stop because it would want another double value but the next thing is Eric’s name. Input mismatch exception.
11
Hours answer // Processes an employee input file and outputs each employee's // hours. import java.io.*; // for File import java.util.*; // for Scanner public class Hours { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("hours.txt")); while (input.hasNextLine()) { String line = input.nextLine(); Scanner lineScan = new Scanner(line); int id = lineScan.nextInt(); // e.g. 456 String name = lineScan.next(); // e.g. "Eric" double sum = 0.0; int count = 0; while (lineScan.hasNextDouble()) { sum = sum + lineScan.nextDouble(); // e.g. 11.6 count++; } double average = sum / count; System.out.printf("%-5s (ID# %d) worked %5.1f hours (%.1f hours/day)\n", name, id, sum, average); } } The point is that we now only process the data on one line at a time. We do not cross line boundaries. Kim (ID# 123) worked 31.4 hours (7.9 hours/day) Eric (ID# 456) worked 36.8 hours (7.4 hours/day) Stef (ID# 789) worked 39.5 hours (7.9 hours/day)
12
12 Line processing example Example: Read in a file containing HTML text, and surround all uppercase tokens with. Assume any token that is all upper case is an HTML tag, so BODY is translated to. – Retain the original order of the tokens on each line. Scanner input = new Scanner(new File("page.html")); while (input.hasNextLine()) { String line = input.nextLine(); Scanner lineScan = new Scanner(line); while (lineScan.hasNext()) { String token = lineScan.next(); if (token.equals(token.toUpperCase())) { // an HTML tag System.out.print(" "); } else { System.out.print(token + " " ); } System.out.println(); } Input file: HTML HEAD TITLE My web page /TITLE /HEAD BODY P There are pics of my cat here, as well as my B cool /B blog, with pics from my Vegas trip. /BODY /HTML Output to console: My web page There are pics of my cat here, as well as my cool blog, with pics from my Vegas trip.
13
import java.io.*; // for File import java.util.*; // for Scanner public class HTMLTokenizer { public static void main(String[] args)throws FileNotFoundException { Scanner input = new Scanner(new File("page.html")); while (input.hasNextLine()) { String line = input.nextLine(); Scanner lineScan = new Scanner(line); while (lineScan.hasNext()) { String token = lineScan.next(); if (token.equals(token.toUpperCase())) { // an HTML tag System.out.print(" "); } else { System.out.print(token + " " ); } System.out.println(); } Output: My web page There are pics of my cat here, as well as my cool blog, with pics from my Vegas trip. Contents of page.html: HTML HEAD TITLE My web page /TITLE /HEAD BODY P There are pics of my cat here, as well as my B cool /B blog, with pics from my Vegas trip. /BODY /HTML This just shows the entire HTMLTokenizer program.
14
14 Complex input question Write a program that searches for and finds the hours worked and average hours per day for a particular person represented in the following file. If the name is not found, it displays a statement to that effect. It can only search for one person on each run of the program. – Input file contents: 123 Susan 12.5 8.1 7.6 3.2 456 Brad 4.0 11.6 6.5 2.7 12 789 Jennifer 8.0 8.0 8.0 8.0 7.5 7.0 – Example log of execution: Enter a name: Brad Brad (ID#456) worked 36.8 hours (7.4 hours/day) – Example log of execution: Enter a name: Harvey Harvey was not found This program only does one search in one run.
15
15 Complex input answer 1 // This program searches an input file of employees' hours worked // for a particular employee and outputs that employee's hours data. // It reads the entire file to find the name. import java.io.*; // for File import java.util.*; // for Scanner public class HoursWorked { public static void main(String[] args) throws FileNotFoundException { Scanner console = new Scanner(System.in); System.out.print("Enter a name: "); String searchName = console.nextLine(); // e.g. "BRAD" boolean found = false; // a boolean flag Scanner input = new Scanner(new File("hours.txt")); while (input.hasNextLine()) { String line = input.nextLine(); Scanner lineScan = new Scanner(line); int id = lineScan.nextInt(); // e.g. 456 String name = lineScan.next(); // e.g. "Brad" if (name.equalsIgnoreCase(searchName)) { processLine(lineScan, name, id); found = true; // we found that employee! } if (!found) { // found will be true if we ever found the person System.out.println(searchName + " was not found"); } more... This program keeps getting lines from the file even after finding a match.
16
16 Complex input answer 1 (continued) // totals the hours worked by one person and outputs their info public static void processLine(Scanner lineScan, String name, int id) { double sum = 0.0; int count = 0; while (lineScan.hasNextDouble()) { sum += lineScan.nextDouble(); count++; } double average = sum / count; System.out.printf("%-5s (ID# %d) worked %5.1f hours (%.1f hours/day)\n", name, id, sum, average); } } Example log of execution: Enter a name: Brad Brad (ID#456) worked 36.8 hours (7.4 hours/day) Example log of execution: Enter a name: Harvey Harvey was not found
17
17 Complex input answer 2: more complex while loop // This program searches an input file of employees' hours worked // for a particular employee and outputs that employee's hours data. import java.io.*; // for File import java.util.*; // for Scanner public class HoursWorked { public static void main(String[] args) throws FileNotFoundException { Scanner console = new Scanner(System.in); System.out.print("Enter a name: "); String searchName = console.nextLine(); // e.g. "BRAD" boolean found = false; // a boolean flag Scanner input = new Scanner(new File("hours.txt")); while (input.hasNextLine() && !found) { // now stops when name is found. String line = input.nextLine(); Scanner lineScan = new Scanner(line); int id = lineScan.nextInt(); // e.g. 456 String name = lineScan.next(); // e.g. "Brad" if (name.equalsIgnoreCase(searchName)) { processLine(lineScan, name, id); // Same method as in previous found = true; // we found that employee! } if (!found) { // found will be true if we ever found the person System.out.println(searchName + " was not found"); } This is much more efficient than the previous version if the file is large.
18
18 // This program searches an input file of employees' hours worked // for a particular employee and outputs that employee's hours data. import java.io.*; // for File import java.util.*; // for Scanner public class HoursWorkedBreak { public static void main(String[] args) throws FileNotFoundException { Scanner console = new Scanner(System.in); System.out.print("Enter a name: "); String searchName = console.nextLine(); // e.g. "BRAD" boolean found = false; // a boolean flag Scanner input = new Scanner(new File("hours2.txt")); while (input.hasNextLine()) { String line = input.nextLine(); Scanner lineScan = new Scanner(line); int id = lineScan.nextInt(); // e.g. 456 String name = lineScan.next(); // e.g. "Brad" if (name.equalsIgnoreCase(searchName)) { processLine(lineScan, name, id); found = true; break; // we found them! } } if (!found) { // found will be true if we ever found the person System.out.println(searchName + " was not found"); } } } Complex input answer 3: use break statement
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.