1 Building Java Programs Chapter 6 Lecture 6-3: Section Problems reading: 6.3 - 6.5.

Slides:



Advertisements
Similar presentations
Building Java Programs Chapter 6 File Processing Copyright (c) Pearson All rights reserved.
Advertisements

BUILDING JAVA PROGRAMS CHAPTER 6.4 FILE OUTPUT. 22 PrintStream : An object in the java.io package that lets you print output to a destination such as.
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.
1 CSE 142 Lecture Notes File input using Scanner Suggested reading: , Suggested self-checks: Section 6.7 # 1-11, These lecture.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
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 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
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 3: Parameters, Return, and Interactive Programs with Scanner.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
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.
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 6 DETAILS OF TOKEN-BASED PROCESSING.
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:
Interactive Programs with Scanner. 2 Input and System.in interactive program: Reads input from the console. –While the program runs, it asks the user.
Copyright 2008 by Pearson Education Building Java Programs Chapter 6: File Processing Lecture 6-2: Advanced file input reading: self-check: #7-11.
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
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( );
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
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.
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
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 Lecture 6-3: Searching Files reading: 6.3, 6.5.
Introduction to programming in java
Building Java Programs Chapter 6 File Processing Copyright (c) Pearson All rights reserved.
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.
Building Java Programs
Building Java Programs
Building Java Programs Chapter 6
Lecture 3: Input/Output
Building Java Programs
Hours question Given a file hours.txt with the following contents:
Line-based file processing
Topic 20 more file processing
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
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
Optional Topic: User Input with Scanner
Presentation transcript:

1 Building Java Programs Chapter 6 Lecture 6-3: Section Problems reading:

2 Recall: Line-based methods nextLine consumes from the input cursor to the next \n. Scanner input = new Scanner(new File(" ")); while (input.hasNextLine()) { String line = input.nextLine(); ; } MethodDescription nextLine() returns the next entire line of input hasNextLine() returns true if there are any more lines of input to read (always true for console input)

3 What's wrong with this? 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(); int id = line.nextInt(); // e.g. 456 String name = line.nextLine(); // e.g. "Greg" double sum = 0.0; int count = 0; while (line.hasNextDouble()) { sum = sum + line.nextDouble(); count++; } double average = sum / count; System.out.println(name + " (ID#" + id + ") worked " + sum + " hours (" + average + " hours/day)"); }

4 Corrected code 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 tokens = new Scanner(line); int id = tokens.nextInt(); // e.g. 456 String name = tokens.next(); // e.g. "Greg" double sum = 0.0; int count = 0; while (tokens.hasNextDouble()) { sum = sum + tokens.nextDouble(); count++; } double average = sum / count; System.out.println(name + " (ID#" + id + ") worked " + sum + " hours (" + average + " hours/day)"); }

5 Recall: Tokenizing lines A String Scanner can tokenize each line of a file. Scanner input = new Scanner(new File(" filename ")); while (input.hasNextLine()) { String line = input.nextLine(); Scanner tokens = new Scanner(line); ; }

6 collapseSpaces exercise Write a method collapeSpaces that accepts a file Scanner and writes that file's text to the console, with multiple spaces/tabs between words reduced to a single space. file input four score and seven years ago our fathers brought forth on this continent a new nation console output four score and seven years ago our fathers brought forth on this continent a new nation

7 Exercise solution public static void collapseSpaces(Scanner input) { while (input.hasNextLine()) { String line = input.nextLine(); Scanner words = new Scanner(line); if (words.hasNext()) { System.out.print(words.next()); while (words.hasNext()) { System.out.print(" " + words.next()); } System.out.println(); }

8 leetSpeak exercise Write a method leetSpeak that accepts two parameters: a Scanner representing an input file, and a PrintStream representing an output file. Convert the input file's text to "leet speak", where various letters are replaced by other letters/numbers. Output the leet version of the text to the given output file. Preserve the original line breaks from the input. Wrap each word of input in parentheses. // example call Scanner input = new Scanner(new File("lincoln.txt")); PrintStream output = new PrintStream(new File("leet.txt")); leetSpeak(input, output);

speak replacements Original character 'Leet' character Original character 'Leet' character o0a4 l (lowercase L) 1t7 e3 s (at end of word) Z Input fileOutput file four score and seven years ago our fathers brought forth on this continent a new nation (f0ur) (sc0r3) (4nd) (s3v3n) (y34rZ) (4g0) (0ur) (f47h3rZ) (br0ugh7) (f0r7h) (0n) (7hiZ) (c0n7in3n7) (4) (n3w) (n47i0n)

10 Exercise solution public static void leetSpeak( Scanner input, PrintStream output ) { while (input.hasNextLine()) { String line = input.nextLine(); Scanner lineScanner = new Scanner(line); while (lineScanner.hasNext()) { String word = lineScanner.next(); word = word.replace("o", "0"); word = word.replace("l", "1"); word = word.replace("e", "3"); word = word.replace("a", "4"); word = word.replace("t", "7"); if (word.endsWith("s")) { word = word.substring(0, word.length() - 1) + "Z"; } output.print("(" + word + ") "); } output.println(); // preserve line breaks }

11 Mixing tokens and lines Using nextLine in conjunction with the token-based methods on the same Scanner can cause bad results Joe "Hello world" You'd think you could read 23 and 3.14 with nextInt and nextDouble, then read Joe "Hello world" with nextLine. System.out.println(input.nextInt()); // 23 System.out.println(input.nextDouble()); // 3.14 System.out.println(input.nextLine()); // But the nextLine call produces no output! Why?

12 Mixing lines and tokens Don't read both tokens and lines from the same Scanner : Joe "Hello world" input.nextInt() // 23 23\t3.14\nJoe\t"Hello world"\n\t\t \n ^ input.nextDouble() // \t3.14\nJoe\t"Hello world"\n\t\t \n ^ input.nextLine() // "" (empty!) 23\t3.14\nJoe\t"Hello world"\n\t\t \n ^ input.nextLine() // "Joe\t\"Hello world\"" 23\t3.14\nJoe\t"Hello world"\n\t\t \n ^

13 Line-and-token example Scanner console = new Scanner(System.in); System.out.print("Enter your age: "); int age = console.nextInt(); System.out.print("Now enter your name: "); String name = console.nextLine(); System.out.println(name + " is " + age + " years old."); Log of execution (user input underlined): Enter your age: 12 Now enter your name: Sideshow Bob is 12 years old. Why? Overall input: 12\nSideshow Bob After nextInt(): 12\nSideshow Bob ^ After nextLine():12\nSideshow Bob ^