CSCI 1103: File I/O, Scanner, PrintWriter

Similar documents
CSCI 1103: File I/O, Scanner, PrintWriter

CS 211: Existing Classes in the Java Library

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

Building Java Programs

Reading Input from Text File

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

Excep&ons and file I/O

Object-Oriented Programming in Java

Building Java Programs

Object Oriented Programming. Java-Lecture 1

Fundamentals of Programming Data Types & Methods

EXCEPTIONS. Fundamentals of Computer Science I

Computer Science is...

Building Java Programs

Building Java Programs

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

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

A+ Computer Science -

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

COMP102: 181. Menu. More while loops. Admin: Test. Peter Andreae

File I/O Array Basics For-each loop

Midterm Examination (MTA)

Text User Interfaces. Keyboard IO plus

CSCI 161 Introduction to Computer Science

CS 211: Basic Inheritance

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

Building Java Programs

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

File I/O Introduction to File I/O Text Files The File Class Binary Files 614

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Programming with Java

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

Building Java Programs Chapter 6

Building Java Programs

COMP102: Test 2 Model Solutions

CS 211: Inheritance. Chris Kauffman. Week 5-2

Chapter 4: Loops and Files

COMP102: Test 1 Model Solutions

Computational Expression

Java Input/Output. 11 April 2013 OSU CSE 1

H212 Introduction to Software Systems Honors

Adam Blank Lecture 2 Winter 2019 CS 2. Introduction to Programming Methods

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

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

Reading Text Files. 1 Reading from text files: Scanner

Building Java Programs

CS244 Advanced Programming Applications

Chapter 4: Loops and Files

Introduction to Java Applications

F I N A L E X A M I N A T I O N

CS111: PROGRAMMING LANGUAGE II

CS 1301 Ch 8, Handout 3

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

Project 1. Java Data types and input/output 1/17/2014. Primitive data types (2) Primitive data types in Java

AP Computer Science Unit 1. Writing Programs Using BlueJ

CS 112 Introduction to Programming

Agenda & Reading. Python Vs Java. COMPSCI 230 S Software Construction

Lecture 8 " INPUT " Instructor: Craig Duckett

CS 112 Introduction to Programming

Robots. Byron Weber Becker. chapter 6

Chapter 10. File I/O. Copyright 2016 Pearson Inc. All rights reserved.

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Classes and Objects Miscellany: I/O, Statics, Wrappers & Packages. CMSC 202H (Honors Section) John Park

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

More on Java. Object-Oriented Programming

Object Oriented Programming. Java-Lecture 6 - Arrays

Full file at

CSCI 1103: Basics of Recursion

CS 112 Introduction to Programming

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

Topic 11 Scanner object, conditional execution

COMP102: Test. 26 April, 2006

CSci 1103 Final. Name: Student ID:

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

COSC 123 Computer Creativity. I/O Streams and Exceptions. Dr. Ramon Lawrence University of British Columbia Okanagan

Section 2.2 Your First Program in Java: Printing a Line of Text

Files & Exception Handling. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

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

ing execution. That way, new results can be computed each time the Class The Scanner

CS1083 Week 2: Arrays, ArrayList

Mandatory Assignment 1, INF 4130, 2017

STUDENT LESSON A7 Simple I/O

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Full file at

CMPT 125: Lecture 4 Conditionals and Loops

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

AP Computer Science Unit 1. Programs

Elementary Programming

BlueJ Demo. Topic 1: Basic Java. 1. Sequencing. Features of Structured Programming Languages

Simple Java Reference

Java Console Input/Output The Basics

Exception Handling. CSE 114, Computer Science 1 Stony Brook University

Repetition. Chapter 6

Repetition. Chapter 6

When we reach the line "z = x / y" the program crashes with the message:

Files. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Files. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

A Quick and Dirty Overview of Java and. Java Programming

Transcription:

CSCI 1103: File I/O, Scanner, PrintWriter Chris Kauffman Last Updated: Mon Dec 4 10:03:11 CST 2017 1

Logistics Reading from Eck Ch 2.1 on Input, File I/O Ch 11.1-2 on File I/O Goals Scanner for input Input from different sources File output with PrintWriter Lab12: Scanner Exercises on reading input from different sources Project 5 Will post in next two days Due last few days of class 2

Input / Output (I/O) so far Input So far have used the TextIO to get input from users TextIO was written by Eck (text author) for beginners: not part of standard Java Scanner is a similar class which is part of the standard Java library Will study Scanner to read from user, String, and File Output So far only System.out.println() / printf() to put stuff on the screen Can also put stuff in files for permanent storage Will use PrintWriter for that 3

The Scanner Class On object which reads input from some source Consider demo program below: similar to TextIO.java 1 // Short demo of the scanner class which reads an integer, double, a 2 // string from the user 3 4 import java.util.scanner; // notify compiler of Scanner use 5 6 public class ScannerDemo{ 7 public static void main(string args[]){ 8 System.out.println("Enter int double string:"); 9 Scanner input = new Scanner(System.in); // scan from keyboard 10 int i = input.nextint(); // read an int 11 double d = input.nextdouble(); // read a double 12 String s = input.next(); // read a string 13 String s2 = input.next(); // read a string 14 System.out.printf("%d %f %s %s\n",i,d,s,s2); 15 } 16 } Where could one find out about all the methods of Scanner? 4

Imports and Packages Java s library has MANY classes divided into packages Essential classes like String, System, Math live in the java.lang package which is always imported by default Less essential classes like Scanner live elsewhere such as in the java.util package Other packages must be imported with lines like import java.util.scanner; // notify compiler of Scanner use import java.io.file; // notify compiler of File use import java.util.arraylist; // notify compiler ArrayList use import java.util.*; Packages are a complex topic for another day // using multiple classes from package 5

Detecting the End of Input class Scanner{ public String next() // Finds and returns the next complete token from // this scanner. public boolean hasnext() // Returns true if this scanner has another token // in its input. CK: safe to call next() In input loops, make use of hasnext() to determine if more input is present For interactive input, special characters can be used to signal end of input Mac/Unix: press Ctrl-D to signal end of input Windows: press Ctrl-Z to signal end of input 6

Sample of Detecting End of Input 1 // Demonstrate the hasnext() method of Scanner 2 import java.util.scanner; 3 4 public class EndOfInput{ 5 public static void main(string args[]){ 6 System.out.println("To end input"); 7 System.out.println("- Mac/Unix: press Ctrl-D"); 8 System.out.println("- Windows: press Ctrl-Z"); 9 Scanner input = new Scanner(System.in); 10 int count = 0; 11 String allstrings = ""; 12 13 // System.out.println("Enter a string:"); 14 while(input.hasnext()){ // loop while input 15 System.out.println("Enter a string:"); 16 String str = input.next(); // get a string 17 count++; // increment 18 allstrings += str + "\n"; // tack on to all 19 //System.out.println("Enter a string:"); 20 } 21 22 System.out.printf("End of input reached\n"); 23 System.out.printf("%d total strings typed\n",count); 24 System.out.printf("All strings:\n%s\n",allstrings); 25 } 26 } 7

Exercise: Sum Integers Repeatedly prompt for input Use nextint() to read an int Add on to a total Go until end of input is indicated Use hasnext() for this > javac SumIntegers.java > java SumIntegers Stop with Ctrl-D (Mac/Unix) or Ctrl-Z Enter an integer: 12 Enter an integer: 14 Enter an integer: 1 Enter an integer: 9 Enter an integer: End of input Total: 36 8

Answer: Sum Integers 1 // Exercise to sum numbers 2 import java.util.scanner; 3 4 public class SumIntegers{ 5 public static void main(string args[]){ 6 System.out.println("Stop with Ctrl-D (Mac/Unix) or Ctrl-Z (Windows)"); 7 8 Scanner input = new Scanner(System.in); 9 int total = 0; 10 System.out.println("Enter an integer:"); 11 while(input.hasnext()){ // loop while input 12 int num = input.nextint(); // get a number 13 total += num; // increment 14 System.out.println("Enter an integer:"); 15 } 16 System.out.printf("End of input\n"); 17 System.out.printf("Total: %d\n",total); 18 } 19 } 9

Constructors for Scanner Scanners are initialized with a source from which they read Mostly behave identically irrespective of a source: useful // READ FROM THE TERMINAL // Constructs a new Scanner that produces values // scanned from the specified input stream. Scanner in = new Scanner(System.in); // READ FROM A STRING // Constructs a new Scanner that produces values // scanned from the specified string. Scanner in = new Scanner("Give me a 1 Give me a 2"); // READ FROM A FILE // Constructs a new Scanner that produces values // scanned from the specified File. Scanner in = new Scanner(new File("stuff.txt")); //!! Oh - that IS interesting 10

Sum integers from Strings and Files 1 // Demo to sum integers from 2 // different strings 3 import java.util.scanner; 4 5 public class SumStringInts{ 6 public static void main(string args[]){ 7 Scanner input; 8 int total; 9 10 String nums1 = "1 2 3 4"; 11 input = new Scanner(nums1); 12 total = 0; 13 while(input.hasnext()){ 14 int num = input.nextint(); 15 total += num; 16 } 17 System.out.printf("Total: %d\n", 18 total); 19 20 String nums2 = "10 30 50 70 90"; 21 input = new Scanner(nums2); 22 total = 0; 23 while(input.hasnext()){ 24 int num = input.nextint(); 25 total += num; 26 } 27 System.out.printf("Total: %d\n", 28 total); 29 } 30 } 1 // Demo to sum integers from a file 2 import java.util.scanner; 3 import java.io.file; // using File class 4 5 public class SumFileInts{ 6 public static void main(string args[]){ 7 try{ 8 String filename = "numbers.txt"; 9 Scanner input = 10 new Scanner(new File(filename)); 11 int total = 0; 12 while(input.hasnext()){ 13 int num = input.nextint(); 14 total += num; 15 } 16 System.out.printf("Total: %d\n", 17 total); 18 input.close(); // for file scanning 19 } 20 catch(exception e){ 21 throw new RuntimeException(e); 22 } 23 } 24 } What looks ugly and interesting in these examples? 11

Exceptions when Reading from Files Things can go wrong with File operations Typically throws a checked exception: one that must be caught or declared Will see methods mentioning checked exceptions they throw public void amethod(...) throws KersplosionException ^^^^^^^^^^^^^^^^^^^^^^^^^^^ This is more complex than needed for our cases: don t want to catch OR declare so we will often take the following unsavory approach public void somemethod(...){ try {... Scanner input = new Scanner(new File(filename));... } catch(exception e){ // if an exception results throw new RuntimeException(e); // re-throw as a RuntimeException } } 12

Common Pattern Lots of repeated code to loop and sum Really want one spot for this: a method like public static int sumall(scanner input) // read all integers in input and return their sum Could then pass in an open scanner as in Scanner input; input = new Scanner("1 2 3"); int sum1 = sumall(input); input = new Scanner("4 5 6 7"); int sum2 = sumall(input); input = new Scanner(new File(fname)); int sum3 = sumall(input); input.close(); 13

Exercise: Pick a File to Sum Have several files like numbers.txt, scores.txt, scores2.txt Want to pick which one to sum up How could this be done using Scanner? Hint: Using multiple Scanners with different sources may help 14

Answer: Pick a File to Sum See SumPickFile.java. Central technique: read from two places Scanner userinput = new Scanner(System.in); // to read typed input System.out.println("Enter a filename:"); // prompt String filename = userinput.next(); // get filename // to read file input Scanner fileinput = new Scanner(new File(filename)); int total = sumall(fileinput); fileinput.close(); System.out.printf("Total: %d\n", total); // method to sum all The pattern of specifying a file for a program to operate on is very common It is much more common to give this as a command line argument so it appears in main(string args[]) Command line args be discussed soon 15

Scanning Errors 1 // Demo of scanning errors 2 import java.util.scanner; 3 4 public class ScanError{ 5 public static void main(string args[]){ 6 String nums = "5 4 three 2 1"; 7 Scanner input = new Scanner(nums); 8 int total = 0; 9 while(input.hasnext()){ 10 int num = input.nextint(); 11 System.out.printf("read %d\n",num); 12 total += num; 13 } 14 System.out.printf("Total: %d\n", total); 15 } 16 } Scanners read what they are told and throw exceptions if problems arise Next input can be checked with methods like hasnextint() and hasnextdouble() Careful with these as loop conditions > javac ScanError.java > java ScanError read 5 read 4 Exception in thread "main" java.util.inputmismatchexception at java.util.scanner.throwfor(scanner.java:864) at java.util.scanner.next(scanner.java:1485) at java.util.scanner.nextint(scanner.java:2117) at java.util.scanner.nextint(scanner.java:2076) at ScanError.main(ScanError.java:10) 16

Scanner Input Method Summary Method new Scanner(System.in) new Scanner("hi 1 2.3") new Scanner(new File(fname)) close() String next() int nextint() double nextdouble() String nextline() boolean hasnext() boolean hasnextint() boolean hasnextdouble() boolean hasnextline() Purpose scan from terminal input scan from string scan from file when reading files get next String get next integer get next double read a whole line true if more to read true if next is an int true if next is a double true there is another line 17

PrintWriter for File Output A class that allows printing to the screen or to a file Constructing a PrintWriter with a File argument allows printing into that named file Similar to constructing a Scanner from a File argument reads from the file Methods of PrintWriter are familiar: println() / printf() along with close() PrintWriter fileout = new PrintWriter(new File("myfile.txt")); fileout.println("here is text"); fileout.printf("numbers: %d %f\nstrings: %s\n", 123, 4.56, "hello!"); fileout.close(); Have a look at the PrintWriter Java Doc. 18

File Output Example 1 // Show PrintWriter use to create a text file with a table 2 import java.util.scanner; 3 import java.io.printwriter; 4 import java.io.file; 5 6 public class PrintTable{ 7 public static void main(string args[]) throws Exception{ 8 Scanner input = new Scanner(System.in); 9 System.out.println("File to save in (ex: stuff.txt):"); 10 String filename = input.next(); 11 System.out.println("Height and Width of table (ex: 4 6):"); 12 int height = input.nextint(); 13 int width = input.nextint(); 14 15 PrintWriter fileout = new PrintWriter(new File(fileName)); 16 for(int row=1; row<=height; row++){ 17 for(int col=1; col<=width; col++){ 18 fileout.printf("%3d ",row * col); 19 } 20 fileout.println(""); 21 } 22 fileout.close(); 23 24 System.out.printf("%d by %d table saved in %s\n", 25 height,width,filename); 26 } 27 } 19

Exercise: File Output Example Examine PrintTable.java 1. Identify where a PrintWriter is created and how 2. Explain what methods are used to print output into the file and whether they are familiar or unfamiliar 3. Which method terminates output to that file? 4. How is the name of the output file and table size determined? 5. Explain a different way the filename/table size could be obtained at startup time based on last Friday s lecture Hint: you may need to use the Integer.parseInt(i) method to convert a String to an int 20

Answers: File Output Example 1. Identify where a PrintWriter is created and how PrintWriter fileout = new PrintWriter(new File(fileName)); 2. Explain what methods are used to print output into the file and whether they are familiar or unfamiliar fileout.printf("%3d ",row * col); fileout.println(""); Both familiar from use with System.out 3. Which method terminates output to that file? fileout.close(); 4. How is the name of the output file and table size determined? A: A Scanner is used to read them from the user 5. Explain a different way the filename/table size could be obtained at startup time based on last Friday s lecture A: They could be specified on the command line and grabbed from the args[] array in main() public static void main(string args[]) throws Exception{ String filename = args[0]; int height = Integer.parseInt(args[1]); int width = Integer.parseInt(args[2]); See PrintTableCmdArg.java 21