Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lab : File I/O Copyright © 2012 Pearson Education, Inc. Chapter 2.6 GETTING USER INPUT WITH SCANNER Copyright © 2012 Pearson Education, Inc. Interactive Programs • The Scanner class provides convenient methods for reading input • A Scanner object can be set up to read input from various sources, from a user typing values on the keyboard to parsing a string or file • Keyboard input is represented by System.in Copyright © 2012 Pearson Education, Inc. Reading Input • The following line creates a Scanner object that reads from the keyboard: Scanner scan = new Scanner (System.in); • Once created, the Scanner object can be used to invoke various input methods, such as: answer = scan.nextLine(); • The nextLine method reads all of the input until the end of the line is found Copyright © 2012 Pearson Education, Inc. //******************************************************************** // Echo.java Author: Lewis/Loftus // // Demonstrates the use of the nextLine method of the Scanner class // to read a string from the user. //******************************************************************** import java.util.Scanner; public class Echo { //----------------------------------------------------------------// Reads a character string from the user and prints it. //----------------------------------------------------------------public static void main (String[] args) { String message; Scanner scan = new Scanner (System.in); System.out.println ("Enter a line of text:"); message = scan.nextLine(); System.out.println ("You entered: \"" + message + "\""); } } Copyright © 2012 Pearson Education, Inc. Sample Run //******************************************************************** // Echo.java Author: Lewis/Loftus Enter a line of text: // // Demonstrates the fries use of the nextLine method of the Scanner class You want with that? // to read a string from the user. You entered: "You want fries with that?" //******************************************************************** import java.util.Scanner; public class Echo { //----------------------------------------------------------------// Reads a character string from the user and prints it. //----------------------------------------------------------------public static void main (String[] args) { String message; Scanner scan = new Scanner (System.in); System.out.println ("Enter a line of text:"); message = scan.nextLine(); System.out.println ("You entered: \"" + message + "\""); } } Copyright © 2012 Pearson Education, Inc. Input Tokens • Unless specified otherwise, white space is used to separate the elements (called tokens) of the input • White space includes space characters, tabs, new line characters • The next method of the Scanner class reads the next input token and returns it as a string • Methods such as nextInt and nextDouble read data of particular types • See GasMileage.java Copyright © 2012 Pearson Education, Inc. //******************************************************************** // GasMileage.java Author: Lewis/Loftus // // Demonstrates the use of the Scanner class to read numeric data. //******************************************************************** import java.util.Scanner; public class GasMileage { //----------------------------------------------------------------// Calculates fuel efficiency based on values entered by the // user. //----------------------------------------------------------------public static void main (String[] args) { int miles; double gallons, mpg; Scanner scan = new Scanner (System.in); continue Copyright © 2012 Pearson Education, Inc. int miles; double gallons, mpg; continue System.out.print ("Enter the number of miles: "); miles = scan.nextInt(); System.out.print ("Enter the gallons of fuel used: "); gallons = scan.nextDouble(); mpg = miles / gallons; System.out.println ("Miles Per Gallon: " + mpg); } } Copyright © 2012 Pearson Education, Inc. int miles; double gallons, mpg; continue System.out.print ("Enter the number of miles: "); miles = scan.nextInt(); System.out.print ("Enter the gallons of fuel used: "); gallons = scan.nextDouble(); mpg = miles / gallons; System.out.println ("Miles Per Gallon: " + mpg); } } Sample Run Enter the number of miles: 328 Enter the gallons of fuel used: 11.2 Miles Per Gallon: 29.28571428571429 Copyright © 2012 Pearson Education, Inc. SCANNING WITH LOOPS Copyright © 2012 Pearson Education, Inc. While Loop Sentinel Template (File Input): setup file scanner while (there is more input){ get input statements to be repeated } File Reading Example: ArrayList<String> lines = new ArrayList<String>(); Scanner s = new Scanner(new File(“in.txt”)); while (s.hasNext()){ String line = s.nextLine(); // get input System.out.println(line); // print line OR lines.add(line); // add line to array list } Copyright © 2012 Pearson Education, Inc. Practice Loops & File IO (Scanner) • Step 1: Create a project named FileScreenPrinter, with a class FileScreenPrinter • Step 2: Create a file named phrases.txt in the FileScreenPrinter project that contains phrases • Step 3: Store all the phrases into an ArrayList • Step 4: Write a main that reads in a file & prints out each line extracting it from the collection, not from the file. Be sure to close the file Scanner Copyright © 2012 Pearson Education, Inc.