Download presentation
Presentation is loading. Please wait.
Published byProsper Warren Modified over 9 years ago
1
1 Line-based file processing suggested reading:6.3
2
2 Line-by-line processing Scanners have a method named nextLine that returns text from the input cursor's current position forward to the nearest \n new line character. You can use nextLine to break up a file's contents into each line and examine the lines individually. Reading a file line-by-line, general syntax: Scanner input = new Scanner(new File(" ")); while (input.hasNextLine()) { String line = input.nextLine(); ; }
3
3 Line-based input The Scanner's nextLine method consumes and returns an entire line of input as a String. The Scanner moves its cursor until it sees a \n new line character, and returns all text that was found. The \n character is consumed but not returned. nextLine is the only non-token-based Scanner method. Recall that the Scanner also has a hasNextLine method. Example: 23 3.14 John Smith "Hello world" 45.219 String line1 = input.nextLine(); 23\t3.14 John Smith\t"Hello world"\n\t\t45.2 19\n ^ String line2 = input.nextLine(); 23\t3.14 John Smith\t"Hello world"\n\t\t45.2 19\n ^
4
4 File processing problem Write a program that reads an email message from a text file and turns it into a quoted message by putting a > and space in front of each line. Example input: Kelly, 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, Joe Example output: > Kelly, > > 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, Joe
5
5 File processing answer The following code solves the problem: import java.io.*; import java.util.*; public class QuoteMessage { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("message.txt")); while (input.hasNextLine()) { String line = input.nextLine(); System.out.println(">" + line); }
6
6 Processing tokens of one line Many input files have a data record on each line. The contents of each line contain meaningful tokens. Example 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 Consider the task of computing the total hours worked for each person represented in the above file. Enter a name: Brad Brad (ID#456) worked 36.8 hours (7.36 hours/day) Neither line-based nor token-based processing is quite right, because we want to break the input into tokens. The better solution is a hybrid approach in which we break the input into lines, and then break each line into tokens.
7
7 Scanners on Strings A Scanner can be constructed to tokenize a particular String, such as one line of an input file. Scanner = new Scanner( ); Example: String text = "1.4 3.2 hello 9 27.5"; Scanner scan = new Scanner(text); // five tokens We can use this idea to tokenize each line of a file. Scanner input = new Scanner(new File(" ")); while (input.hasNextLine()) { String line = input.nextLine(); Scanner lineScan = new Scanner(line); ; }
8
8 Complex input question Write a program that computes the total hours worked and average hours per day for a particular person represented in the following file. 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.36 hours/day)
9
9 Searching for a line Recall: reading a file line-by-line, general syntax: Scanner input = new Scanner(new File(" ")); while (input.hasNextLine()) { String line = input.nextLine(); Scanner lineScan = new Scanner(line); ; } If we are looking for a particular line, often we look for the token(s) of interest on each line. If we find the right value, we'll process the rest of the line. Example: If the second token on the line is "Brad", process it.
10
10 Processing a particular line The following code solves the "hours" problem: Scanner console = new Scanner(System.in); System.out.print("Enter a name: "); String name = console.next(); Scanner input = new Scanner(new File("hours.txt")); while (input.hasNextLine()) { String line = input.nextLine(); Scanner lineScan = new Scanner(line); int id = lineScan.nextInt(); String thisName = lineScan.next(); // "Brad" double sum = 0.0; int count = 0; if (name.equals(thisName)) { // we found the right person while (lineScan.hasNextDouble()) { sum += lineScan.nextDouble(); count++; } double avg = sum / count; System.out.println(name + " worked " + sum + " hours (" + avg + " hours/day)"); }
11
11 File processing question Write a program that reads in a file containing pseudo- HTML text, but with the HTML tags missing their brackets. Whenever you see any uppercase token in the file, surround it with, and output the corrected file text to the console. You must retain the original orientation/spacing of the tokens on each line. (Is this problem line-based or token-based?) 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, which contains I awesome /I stuff about my trip to Vegas. /BODY /HTML Output to console: My web page There are pics of my cat here, as well as my cool blog, which contains awesome stuff about my trip to Vegas.
12
12 File processing solution The following code solves the HTML problem: import java.io.*; import java.util.*; public class WebPage { 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())) { // this is an HTML tag System.out.print(" "); } else { System.out.print(token + " " ); } System.out.println(); }
13
13 Prompting for a file name Instead of writing the file's name into the program as a String, we can ask the user to tell us the file to use. Here is one case where we may wish to use the nextLine method on the console Scanner, because the file name might have spaces in it. // prompt for the file name Scanner console = new Scanner(System.in); System.out.print("Type a file name to use: "); String filename = console.nextLine(); Scanner input = new Scanner(new File(filename));
14
14 Fixing file-not-found issues What if the user types a file name that does not exist? File objects have an exists method we can use to make sure that the file is found on the system. Scanner console = new Scanner(System.in); System.out.print("Type a file name to use: "); String filename = console.nextLine(); File file = new File(filename); while (!file.exists()) { System.out.print("File not found! Try again: "); String filename = console.nextLine(); file = new File(filename); } Scanner input = new Scanner(file); // open the file Output: Type a file name to use: hourz.text File not found! Try again: h0urz.txt File not found! Try again: hours.txt
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.