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.

Slides:



Advertisements
Similar presentations
More Java Syntax. Scanner class Revisited The Scanner class is a class in java.util, which allows the user to read values of various types. There are.
Advertisements

Building Java Programs Chapter 6 File Processing Copyright (c) Pearson All rights reserved.
Copyright 2008 by Pearson Education Line-based file processing reading: 6.3 self-check: #7-11 exercises: #1-4, 8-11.
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.
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.
1 CSE 142 Lecture Notes File input using Scanner Suggested reading: , Suggested self-checks: Section 6.7 # 1-11, These lecture.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
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.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 6: File Processing.
1 CSE 142 Lecture Notes Interactive Programs with Scanner Chapter 4 Suggested reading: Suggested self-checks: Section 4.10 #1-4 These lecture.
Topic 20 more file processing Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
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.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
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 6 DETAILS OF TOKEN-BASED PROCESSING.
Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦
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:
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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:
File Processing Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from "We can only.
1 Line-based file processing suggested reading:6.3.
CS1020 Data Structures and Algorithms I Lecture Note #16 File Processing.
Copyright 2008 by Pearson Education Building Java Programs Chapter 6 Lecture 6-3: Searching Files reading: 6.3, 6.5.
Building Java Programs Chapter 6 File Processing Copyright (c) Pearson All rights reserved.
Building Java Programs
Building Java Programs
Building Java Programs Chapter 6
Output to files reading: 6.4.
Building Java Programs
Building Java Programs
Hours question Given a file hours.txt with the following contents:
File Input and Output TOPICS File Input Exception Handling File Output.
Chapter 5: Control Structures II
Line-based file processing
Chapter 5: Control Structures II
File Input and Output TOPICS File Input Exception Handling File Output.
Topic 20 more file processing
Building Java Programs
Building Java Programs
Building Java Programs
CS 106A, Lecture 10 File Reading
Building Java Programs
Building Java Programs
Building Java Programs
Chapters 6 and 7 Line-Based File Input, Arrays reading: , 7.1
Building Java Programs
Input/output (I/O) import java.io.*;
Building Java Programs
Building Java Programs
Hours question Given a file hours.txt with the following contents:
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
Presentation transcript:

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.

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 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 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); }

Consuming lines of input John Smith "Hello" world 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\t \n 47 ^ – String line = input.nextLine(); 23\t3.14 John Smith\t"Hello" world\n\t\t \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\t \n 47 ^ line2 now contains \t\t – Each \n character is consumed but not returned as part of the string.

Scanners on Strings A Scanner can tokenize each line ( String) in a file: Scanner name = new Scanner( String ); – Example: String text = " hello "; 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 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 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

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.”.

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 Eric Stef – 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.

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 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 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.

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 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 Brad Jennifer – 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 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 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 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 // 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