Building Java Programs

Similar documents
Building Java Programs

Building Java Programs

Building Java Programs

Building Java Programs

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Building Java Programs

Building Java Programs

Building Java Programs

Building Java Programs

Building Java Programs Chapter 6

Building Java Programs

Building Java Programs

Building Java Programs

Building Java Programs

Array basics. How would you solve this? Arrays. What makes the problem hard? Array auto-initialization. Array declaration. Readings: 7.

Topic 23 arrays - part 3 (tallying, text processing)

Building Java Programs

Files. Reading data from files. File class. Compiler error with files. Checked exceptions. Exceptions. Readings:

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Building Java Programs Chapter 7

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Array basics. Readings: 7.1

Reading Input from Text File

CSE 142, Spring Chapters 6 and 7 Line-Based File Input, Arrays. reading: , 7.1

CS 112 Introduction to Programming

CSCI 1103: File I/O, Scanner, PrintWriter

Building Java Programs

CSCI 1103: File I/O, Scanner, PrintWriter

File Processing. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. A Class for Representing a File

AP Computer Science. File Input with Scanner. Copyright 2010 by Pearson Education

Building Java Programs

Computer Science is...

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

File class in Java. Scanner reminder. File methods 10/28/14. File Input and Output (Savitch, Chapter 10)

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG

Building Java Programs

CS 112 Introduction to Programming

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

Object Oriented Programming. Java-Lecture 1

AP Computer Science. Return values, Math, and double. Copyright 2010 by Pearson Education

Full file at

Building Java Programs

Building Java Programs

Topic 11 Scanner object, conditional execution

Garfield AP CS. User Input, If/Else. Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp!

Building Java Programs

Unit 10: exception handling and file I/O

Computational Expression

Building Java Programs

Input. Scanner keyboard = new Scanner(System.in); String name;

Building Java Programs

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

CS244 Advanced Programming Applications

CS 211: Existing Classes in the Java Library

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

Building Java Programs

Fundamentals of Programming Data Types & Methods

Solution to Section #3 Portions of this handout by Eric Roberts, Mehran Sahami, Marty Stepp, Patrick Young and Jeremy Keeshin

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

A token is a sequence of characters not including any whitespace.

Programming with Java

Question: Total Points: Score:

CIS 110: Introduction to Computer Programming

CS 112 Introduction to Programming

CS 112 Introduction to Programming

Topic 11 Scanner object, conditional execution

Object Oriented Programming. Java-Lecture 6 - Arrays

Text User Interfaces. Keyboard IO plus

Chapter 2: Basic Elements of Java

Full file at

JAVA Ch. 4. Variables and Constants Lawrenceville Press

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Assignment 2.4: Loops

Building Java Programs

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Computational Expression

COMP200 INPUT/OUTPUT. OOP using Java, based on slides by Shayan Javed

-Alfred North Whitehead. Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Arrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals

Full file at

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Object-Oriented Programming in Java

A Quick and Dirty Overview of Java and. Java Programming

Chapter 4: Loops and Files

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Chapter 2 ELEMENTARY PROGRAMMING

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP 202 File Access 1

Elementary Programming

Midterm Examination (MTA)

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Chapter 4: Control Structures I

Chapter 2. Elementary Programming

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

CS 112 Introduction to Programming

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

CSE 143 Lecture 25. I/O Streams; Exceptions; Inheritance. read 9.3, 6.4. slides adapted from Marty Stepp

Building Java Programs

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. File Input and Output

Transcription:

Building Java Programs Chapter 6: File Processing Lecture 6-2: Advanced file input reading: 6.3-6.5 self-check: #7-11 exercises: #1-4, 8-11 Copyright 2009 by Pearson Education

Hours question Given a file hours.txt with the following contents: 123 Susan 12.5 8.1 7.6 3.2 456 Brad 4.0 11.6 6.5 2.7 12 789 Jenn 8.0 8.0 8.0 8.0 7.5 Consider the task of computing hours worked by each person: Susan (ID#123) worked 31.4 hours (7.85 hours/day) Brad (ID#456) worked 36.8 hours (7.36 hours/day) Jenn (ID#789) worked 39.5 hours (7.9 hours/day) Can we produce this data token by token? Copyright 2009 by Pearson Education 2

Hours program strategy 123 Susan 12.5 8.1 7.6 3.2 456 Brad 4.0 11.6 6.5 2.7 12 789 Jenn 8.0 8.0 8.0 8.0 7.5 We want to process the tokens, but we also care about the line breaks (they mark the end of a person's data). A better solution: First, break the overall input into lines. Then break each line into tokens. What if you tried it token by token first? Copyright 2009 by Pearson Education 3

Hours answer (flawed) import java.io.*; import java.util.*; // for File // for Scanner public class HoursWorked { // a non-working solution public static void main(string[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("hours.txt")); while (input.hasnext()) { // process one person int id = input.nextint(); String name = input.next(); double totalhours = 0.0; int days = 0; while (input.hasnextdouble()) { totalhours += input.nextdouble(); days++; System.out.println(name + " (ID#" + id + ") worked " + totalhours + " hours (" + (totalhours / days) + " hours/day)"); Copyright 2009 by Pearson Education 4

Flawed output Susan (ID#123) worked 487.4 hours (97.48 hours/day) Exception in thread "main" java.util.inputmismatchexception at java.util.scanner.throwfor(scanner.java:840) at java.util.scanner.next(scanner.java:1461) at java.util.scanner.nextint(scanner.java:2091) at HoursWorked.main(HoursBad.java:9) The inner while loop is grabbing the next person's ID. Hard to tell it where to stop! The line breaks indicate the end of the data Much easier and more natural to process by asking for a line of input at a time, then only concentrating on that line Copyright 2009 by Pearson Education 5

Tokenizing lines of a file A Scanner object can also be used to process a String Scanner linescan = new Scanner(string); A String Scanner can tokenize each line of a file. Scanner input = new Scanner(new File("file name")); while (input.hasnextline()) { String line = input.nextline(); Scanner linescan = new Scanner(line); process the contents of this line... Copyright 2009 by Pearson Education 6

Hours program solution // Processes an employee input file and outputs each employee's hours data. 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. "Brad" double sum = 0.0; int count = 0; while (linescan.hasnextdouble()) { sum = sum + linescan.nextdouble(); count++; double average = sum / count; System.out.println(name + " (ID#" + id + ") worked " + sum + " hours (" + average + " hours/day)"); Copyright 2009 by Pearson Education 7

File Processing Summary Token-based file processing: Breaks up entire file into tokens to process data Scanner input = new Scanner(new File("midterm.txt")); while(input.hasnext()) {... (use methods from input to process file) Line-based file processing: Breaks up file into lines to process data Scanner input = new Scanner(new File("hours.txt")); while(input.hasnextline()) { String line = input.nextline(); Scanner linescan = new Scanner(line);... (use methods from linescan to process file) Copyright 2009 by Pearson Education 8

Confusion w/ nextline Using nextline in conjunction with the token-based methods on the same Scanner can cause odd results. 23 3.14 Joe "Hello world" 45.2 19 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? Copyright 2009 by Pearson Education 9

Mixing lines and tokens Don't read both tokens and lines from the same Scanner: 23 3.14 Joe "Hello world" 45.2 19 input.nextint() // 23 23\t3.14\nJoe\t"Hello world"\n\t\t45.2 19\n ^ input.nextdouble() // 3.14 23\t3.14\nJoe\t"Hello world"\n\t\t45.2 19\n ^ input.nextline() 23\t3.14\nJoe\t"Hello world"\n\t\t45.2 19\n ^ // "" (empty!) input.nextline() // "Joe\t\"Hello world\"" 23\t3.14\nJoe\t"Hello world"\n\t\t45.2 19\n ^ Copyright 2009 by Pearson Education 10

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? User's overall input: After nextint(): After nextline(): 12\nSideshow Bob 12\nSideshow Bob ^ 12\nSideshow Bob ^ Copyright 2009 by Pearson Education 11

Array traversals, text processing reading: 7.1, 4.4 self-check: Ch. 7 #8, Ch. 4 #19-23 Copyright 2009 by Pearson Education

Text processing text processing: Examining, editing, formatting text. Often involves for loops to break up and examine a String Examples: Count the number of times 's' occurs in a file Find which letter is most common in a file Count A, C, T and Gs in Strings representing DNA strands Copyright 2009 by Pearson Education 13

Strings as arrays Strings are represented internally as arrays. Each character is stored as a value of primitive type char. Strings use 0-based indexes, like arrays. We can write algorithms to traverse Strings. Example: String str = "Mr. E."; index 0 1 2 3 4 5 value 'M' 'r' '.' ' ' 'E' '.' Copyright 2009 by Pearson Education 14

Recall: type char char: A primitive type representing a single character. Values are surrounded with apostrophes: 'a' or '4' or '\n' Access a string's characters with its charat method. String word = console.next(); char firstletter = word.charat(0); if (firstletter == 'c') { System.out.println("That's good enough for me!"); Copyright 2009 by Pearson Education 15

String traversals traversal: An examination of each element of an array. for (int i = 0; i < array.length; i++) { do something with array[i]; Use for loops to examine each character. for (int i = 0; i < string.length(); i++) { do something with string.charat(i); Copyright 2009 by Pearson Education 16

Section attendance problem Consider an input file of course attendance data: 111111101011111101001110110110110001110010100 111011111010100110101110101010101110101101010 110101011011011011110110101011010111011010101 week1 week2 week3 week4 week5 week6 week7 week8 week9 11111 11010 11111 10100 11101 10110 11000 11100 10100 week2 student1 student2 student3 student4 student5 1 1 0 1 0 Each line represents a section (5 students, 9 weeks). 1 means the student attended; 0 means absent. Copyright 2009 by Pearson Education 17

Section attendance problem Write a program that reads the preceding section data file and produces the following output: Section #1: Sections attended: [9, 6, 7, 4, 3] Student scores: [20, 18, 20, 12, 9] Student grades: [100.0, 90.0, 100.0, 60.0, 45.0] Section #2: Sections attended: [6, 7, 5, 6, 4] Student scores: [18, 20, 15, 18, 12] Student grades: [90.0, 100.0, 75.0, 90.0, 60.0] Section #3: Sections attended: [5, 6, 5, 7, 6] Student scores: [15, 18, 15, 20, 18] Student grades: [75.0, 90.0, 75.0, 100.0, 90.0] Copyright 2009 by Pearson Education 18

Data transformations In this problem we go from 0s and 1s to student grades This is called transforming the data. Often each transformation is stored in its own array. We must map between the data and array indexes. Examples: by position (store the i th value we read at index i ) tally (if input value is i, store it at array index i ) explicit mapping (count 'M' at index 0, count 'O' at index 1) Copyright 2009 by Pearson Education 19

Section attendance answer // This program reads a file representing which students attended // which discussion sections and produces output of the students' // section attendance and scores. import java.io.*; import java.util.*; public class Sections { public static void main(string[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("sections.txt")); while (input.hasnextline()) { // process one section String line = input.nextline(); int[] attended = countattended(line); int[] points = computepoints(attended); double[] grades = computegrades(points); results(attended, points, grades); // Produces all output about a particular section. public static void results(int[] attended, int[] points, double[] grades) { System.out.println("Sections attended: " + Arrays.toString(attended)); System.out.println("Sections scores: " + Arrays.toString(points)); System.out.println("Sections grades: " + Arrays.toString(grades)); System.out.println();... Copyright 2009 by Pearson Education 20

Section attendance answer 2... // Counts the sections attended by each student for a particular section. public static int[] countattended(string line) { int[] attended = new int[5]; for (int i = 0; i < line.length(); i++) { char c = line.charat(i); // c == '1' or c == '0' if (c == '1') { // student attended their section attended[i % 5]++; return attended; // Computes the points earned for each student for a particular section. public static int[] computepoints(int[] attended) { int[] points = new int[5]; for (int i = 0; i < attended.length; i++) { points[i] = Math.min(20, 3 * attended[i]); return points; // Computes the percentage for each student for a particular section. public static double[] computegrades(int[] points) { double[] grades = new double[5]; for (int i = 0; i < points.length; i++) { grades[i] = 100.0 * points[i] / 20.0; return grades; Copyright 2009 by Pearson Education 21

File input/output reading: 6.4-6.5 Copyright 2009 by Pearson Education 22

Prompting for a file name We can ask the user to tell us the file to read. The file name might have spaces: use nextline() // 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)); What if the user types a file name that does not exist? Copyright 2009 by Pearson Education 23

Fixing file-not-found issues File objects have an exists method we can use: 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 Copyright 2009 by Pearson Education 24

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. Do not open a file for reading (Scanner) and writing (PrintStream) at the same time. You could overwrite your input file by accident! The result can be an empty file (size 0 bytes). Copyright 2009 by Pearson Education 25

Printing to files, example Printing into an output file, general syntax: PrintStream name = new PrintStream(new File("file name"));... If the given file does not exist, it is created. If the given file already exists, it is overwritten. PrintStream output = new PrintStream(new File("output.txt")); output.println("hello, file!"); output.println("this is a second line of output."); Can use similar ideas about prompting for file names here. Copyright 2009 by Pearson Education 26

PrintStream question Modify our previous Sections program to use a PrintStream to output to the file section_output.txt. Section #1: Sections attended: [9, 6, 7, 4, 3] Student scores: [20, 18, 20, 12, 9] Student grades: [100.0, 90.0, 100.0, 60.0, 45.0] Section #2: Sections attended: [6, 7, 5, 6, 4] Student scores: [18, 20, 15, 18, 12] Student grades: [90.0, 100.0, 75.0, 90.0, 60.0] Section #3: Sections attended: [5, 6, 5, 7, 6] Student scores: [15, 18, 15, 20, 18] Student grades: [75.0, 90.0, 75.0, 100.0, 90.0] Copyright 2009 by Pearson Education 27

PrintStream answer // Section attendance // This version uses a PrintStream for output. import java.io.*; import java.util.*; public class Sections { public static void main(string[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("sections.txt")); PrintStream out = new PrintStream(new File("section_output.txt")); while (input.hasnextline()) { // process one section String line = input.nextline(); int[] attended = countattended(line); int[] points = computepoints(attended); double[] grades = computegrades(points); results(attended, points, grades, out); // Produces all output about a particular section. public static void results(int[] attended, int[] points, double[] grades, PrintStream out) { out.println("sections attended: " + Arrays.toString(attended)); out.println("sections scores: " + Arrays.toString(points)); out.println("sections grades: " + Arrays.toString(grades)); out.println();... Copyright 2009 by Pearson Education 28