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

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 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: File Output; Reference Semantics reading: , 7.1, 4.3, 3.3 self-checks:
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 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
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.
Topic 11 Scanner object, conditional execution 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
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
Advanced File Processing NOVEMBER 21 ST, Objectives Write text output to a file. Ensure that a file can be read before reading it in your program.
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.
1 Building Java Programs Chapter 6 Lecture 6-3: Section Problems reading:
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:
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.
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.
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
Hours question Given a file hours.txt with the following contents:
Line-based file processing
Topic 11 Scanner object, conditional execution
Topic 20 more file processing
Building Java Programs
Building Java Programs Chapter 6
Building Java Programs
Building Java Programs
Building Java Programs
CSc 110, Spring 2018 Lecture 22: Line-Based File Input
Building Java Programs
Chapters 6 and 7 Line-Based File Input, Arrays reading: , 7.1
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
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
Building Java Programs
Presentation transcript:

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( ); Example: String text = " hello "; 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); ; }

2 IMDB movie ratings problem Consider the following Internet Movie Database (IMDB) Top-250 data from a text file in the following format: Shawshank Redemption, The (1994) Godfather: Part II, The (1974) Casablanca (1942) Write a program that prompts the user for a search phrase and displays any movies that contain that phrase. This program will allow you to search the imdb top 250 movies for a particular word. search word? kill RankVotesRatingTitle To Kill a Mockingbird (1962) Kill Bill: Vol. 1 (2003) Kill Bill: Vol. 2 (2004) Killing, the (1956) 4 matches.

3 Graphical IMDB problem Consider making this a graphical program. Expected graphical output: top-left tick mark at (20, 20) ticks 10px tall, 50px apart first red bar t/l corner at (20, 70) 100px apart vertically (max of 5) 1px tall for every 5000 votes 50px wide for each whole ratings point

4 Mixing graphical, text output When solving complex file I/O problems with a mix of text and graphical output, attack the problem in pieces. Do the text input/output and file I/O first: Handle any welcome message and initial console input. Write code to open the input file and print some of the file's data. (Perhaps print the first token of each line, or print all tokens on a given line.) Write code to process the input file and retrieve the record being searched for. Produce the complete and exact text output. Next, begin the graphical output: First draw any fixed items that do not depend on the user input or file results. Lastly draw the graphical output that depends on the search record from the file.

5 More with lines and tokens

6 Mixing line-based with tokens It is not generally recommended to use nextLine in combination with the token-based methods, because confusing results occur Joe "Hello world" int n = console.nextInt(); // 23 23\t3.14\nJoe\t"Hello world"\n\t\t \n ^ double x = console.nextDouble(); // \t3.14\nJoe\t"Hello world"\n\t\t \n ^ // receives an empty line! String line = console.nextLine(); // "" 23\t3.14\nJoe\t"Hello world"\n\t\t \n ^ // Calling nextLine again will get the following complete line String line2 = console.nextLine(); // "Joe\t\"Hello world\"" 23\t3.14\nJoe\t"Hello world"\n\t\t \n ^

7 Line-and-token example Here's another example of the confusing behavior: 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: Marty Stepp is 12 years old. Why? User's overall input: 12\nMarty Stepp After nextInt(): 12\nMarty Stepp ^ After nextLine():12\nMarty Stepp ^

8 Complex multi-line records Sometimes the data in the file consists of multi-line records. The following data represents students' courses. Each student has their name listed on a line, plus a group of courses, listed as a number of units and the student's grade in that course. Erica Kane Greenlee Smythe Ryan Laveree Adam Chandler Adam Chandler, Jr How can we process one or all of these records?

9 File output using PrintStream suggested reading:6.4

10 Output to files PrintStream : An object in the java.io package that lets you print output to a destination such as a file. System.out is also a PrintStream. Any methods you have used on System.out (such as print, println ) will work on every PrintStream. Printing into an output file, general syntax: PrintStream = new PrintStream(new File(" "));... If the given file does not exist, it is created. If the given file already exists, it is overwritten.

11 Printing to files, example Example: PrintStream output = new PrintStream( new File("output.txt")); output.println("Hello, file!"); output.println("This is a second line of output."); You can use similar ideas about prompting for file names here. You should not open a file for reading ( Scanner ) and writing ( PrintStream ) at the same time. The result can be an empty file (size 0 bytes). You could overwrite your input file by accident!