1 BUILDING JAVA PROGRAMS CHAPTER 6 DETAILS OF TOKEN-BASED PROCESSING.

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Advertisements

Building Java Programs Chapter 6 File Processing Copyright (c) Pearson All rights reserved.
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 2008 by Pearson Education Line-based file processing reading: 6.3 self-check: #7-11 exercises: #1-4, 8-11.
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 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.
Copyright 2010 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 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 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.
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
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.
Topic 19 file input, line based Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
BUILDING JAVA PROGRAMS CHAPTER 6 File Processing.
CS 112 Introduction to Programming Program Analysis Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Interactive Programs with Scanner. 2 Input and System.in interactive program: Reads input from the console. –While the program runs, it asks the user.
Topic 18 file input, tokens Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
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:
Building Java Programs File Processing. 2 Input/output (I/O) import java.io.*; Create a File object to get info about a file on your drive. –(This doesn't.
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:
Copyright 2010 by Pearson Education Building Java Programs Chapter 6 Lecture 6-1: File Input with Scanner reading: 6.1 – 6.2, 5.4.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
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
Lecture 3: Input/Output
Building Java Programs
Building Java Programs
File Input and Output TOPICS File Input Exception Handling File Output.
Building Java Programs
File Input and Output TOPICS File Input Exception Handling File Output.
Building Java Programs
Building Java Programs Chapter 6
Building Java Programs Chapter 6
Building Java Programs
Building Java Programs
Input/output (I/O) import java.io.*;
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Topic 18 file input, tokens
Building Java Programs
Building Java Programs
Input/output (I/O) import java.io.*;
Input/output (I/O) import java.io.*;
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Topic 19 file input, line based
Building Java Programs
Input/output (I/O) import java.io.*;
Building Java Programs
Building Java Programs
Chapter 6 Lecture 6-1: File Input with Scanner reading: 6.1 – 6.2, 5.4
Optional Topic: User Input with Scanner
Presentation transcript:

1 BUILDING JAVA PROGRAMS CHAPTER 6 DETAILS OF TOKEN-BASED PROCESSING

2 INPUT TOKENS token: A unit of user input, separated by whitespace. A Scanner splits a file's contents into tokens. If an input file contains the following: "John Smith" The Scanner can interpret the tokens as the following types: TokenType(s) 23int, double, String 3.14double, String "JohnString Smith"String

3 FILES AND INPUT CURSOR Consider a file numbers.txt that contains this text: A Scanner views all input as a stream of characters: 308.2\n \n\n \n 2.8\n ^ input cursor: The current position of the Scanner.

4 CONSUMING TOKENS consuming input: Reading input and advancing the cursor. Calling nextDouble etc. moves the cursor past the current token \n \n\n \n 2.8\n double x = input.nextDouble(); // \n \n\n \n 2.8\n String s = input.next(); // "14.9" 308.2\n \n\n \n 2.8\n ^ ^

5 FILE INPUT QUESTION Recall the input file numbers.txt : Write a program that reads the first 5 values from the file and prints them along with their sum. number = number = 14.9 number = 7.4 number = 2.8 number = 3.9 Sum = 337.2

6 FILE INPUT ANSWER // Displays the first 5 numbers in the given file, // and displays their sum at the end. import java.io.*; // for File import java.util.*; // for Scanner public class Echo { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("numbers.txt")); double sum = 0.0; for (int i = 1; i <= 5; i++) { double next = input.nextDouble(); System.out.println("number = " + next); sum = sum + next; } System.out.printf("Sum = %.1f\n", sum); }

7 SCANNER EXCEPTIONS InputMismatchException You read the wrong type of token (e.g. read "hi" as int ). NoSuchElementException You read past the end of the input. Finding and fixing these exceptions: Read the exception text for line numbers in your code (the first line that mentions your file; often near the bottom): Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:838) at java.util.Scanner.next(Scanner.java:1347) at CountTokens.sillyMethod(CountTokens.java:19) at CountTokens.main(CountTokens.java:6)

8 READING AN ENTIRE FILE Suppose we want our program to process the entire file. (It should work no matter how many values are in the file.) number = number = 14.9 number = 7.4 number = 2.8 number = 3.9 number = 4.7 number = number = 2.8 Sum = 329.3

9 TESTING FOR VALID INPUT Scanner methods to see what the next token will be: These methods do not consume input; they just give information about the next token. Useful to see what input is coming, and to avoid crashes. MethodDescription hasNext() returns true if there are any more tokens of input to read (always true for console input) hasNextInt() returns true if there is a next token and it can be read as an int hasNextDouble( ) returns true if there is a next token and it can be read as a double

10 USING HASNEXT METHODS To avoid exceptions: Scanner console = new Scanner(System.in); System.out.print("How old are you? "); if (console.hasNextInt()) { int age = console.nextInt(); // will not crash! System.out.println("Wow, " + age + " is old!"); } else { System.out.println("You didn't type an integer."); } To detect the end of a file: Scanner input = new Scanner(new File("example.txt")); while (input.hasNext()) { String token = input.next(); // will not crash! System.out.println("token: " + token); }

11 FILE INPUT QUESTION 2 Modify the Echo program to process the entire file: (It should work no matter how many values are in the file.) number = number = 14.9 number = 7.4 number = 2.8 number = 3.9 number = 4.7 number = number = 2.8 Sum = 329.3

12 FILE INPUT ANSWER 2 // Displays each number in the given file, // and displays their sum at the end. import java.io.*; // for File import java.util.*; // for Scanner public class Echo { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("numbers.txt")); double sum = 0.0; while (input.hasNextDouble()) { double next = input.nextDouble(); System.out.println("number = " + next); sum = sum + next; } System.out.printf("Sum = %.1f\n", sum); }

13 FILE INPUT QUESTION 3 Modify the Echo program to handle files that contain non-numeric tokens (by skipping them). For example, it should produce the same output as before when given this input file, numbers2.txt : hello bad stuff oops :-)

14 FILE INPUT ANSWER 3 // Displays each number in the given file, // and displays their sum at the end. import java.io.*; // for File import java.util.*; // for Scanner public class Echo2 { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("numbers2.txt")); double sum = 0.0; while (input.hasNext()) { if (input.hasNextDouble()) { double next = input.nextDouble(); System.out.println("number = " + next); sum = sum + next; } else { input.next(); // throw away the bad token } System.out.printf("Sum = %.1f\n", sum); }

15 WEATHER QUESTION Write a program that reads in temperatures and outputs the coldest and warmest temps. Weather.txt file data

16 WEATHER ANSWER * NOT ALL CODE IS SHOWN public static void main(String[] args) throws FileNotFoundException{ Scanner input = new Scanner(new File("weather.txt")); double max=0; double min=0; while(input.hasNextDouble()){ double current = input.nextDouble(); if (current < min){ min = current; } if (current > max){ max = current; } } System.out.println("The coldest temp in data set was " + min); System.out.println("The warmest temp in data set was " + max); } }