Lab : File I/O
Copyright © 2012 Pearson Education, Inc.
GETTING USER INPUT
WITH SCANNER
Chapter 2.6
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.
//********************************************************************
// 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 + "\"");
}
}
Sample Run
Enter a line of text:
You wan fries with that?
You entered: "You want fries with that?"
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.
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);
}
}
int miles;
double gallons, mpg;
Copyright © 2012 Pearson Education, Inc.
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
int miles;
double gallons, mpg;
Copyright © 2012 Pearson Education, Inc.
SCANNING WITH LOOPS
Copyright © 2012 Pearson Education, Inc.
File Reading Example:
ArrayList lines = new ArrayList();
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
}
While Loop Sentinel Template (File Input):
setup file scanner
while (there is more input){
get input
statements to be repeated
}
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.