Lecture6

Lecture6 - Interactive Programs It is often useful to...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Interactive Programs It is often useful to design a program to read data from the user interactively during execution. Scanner class provides convenient methods for reading user’s input values of various types. A Scanner object can be set up to read input from various sources, including the user typing values on the keyboard or from a file. Scanner scan = new Scanner(System.in); Scanner scan = new Scanner(new FileReader("myFile"));
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Methods of Scanner Class: (1) String nextLine() String answer = scan.nextLine(); A Scanner reads all of the input until the end of the line is found. • The Scanner class is part of the java.util class library, and must be imported into a program to be used. import java.util.Scanner; public class Echo { 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); }}
Background image of page 2
3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Reading Input from Tokens A Scanner breaks its input into elements (tokens) using a white space. The resulting tokens may then be converted into values of different types using the various next methods. A quote 3 3.5 Abraham Lincoln (2) String next() String answer = scan.next(); • reads the next input token and returns it as a string (3) int nextInt() int answer = scan.nextInt(); (4) double nextDouble()
Background image of page 4
5 import java.util.Scanner; public class GasMileage { public static void main (String[] args) { int miles; double gallons, mpg; Scanner scan = new Scanner (System.in); 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); } }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Boolean Methods The Scanner methods that read numeric data throw a InputMismatchException exception if the next value isn't what the method expects. Here are four useful Boolean methods that allow us to check to be sure that the next value is what we expect. Method Returns boolean hasNextLine() Returns true if the scanner has another line in its input; false otherwise. boolean hasNextInt() Returns true if the next token in the scanner can be interpreted as an int value. Boolean hasNextFloat() Returns true if the next toke in the scanner can be interpreted as a float value.
Background image of page 6
7 import java.util.Scanner; public class MixedTypeInput { public static void main(String[] args) { double number; Scanner in = new Scanner(System.in); System.out.println( "Enter your gross income: " ); if (in.hasNextInt()){ number = ( double )in.nextInt(); System.out.println( "You entered " + number); } else if (in.hasNextFloat()){ number = ( double )in.nextFloat(); System.out.println( "You entered " + number); } else if (in.hasNextDouble()){ number = in.nextDouble();
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/12/2010 for the course COE 318 taught by Professor Ken during the Spring '08 term at Ryerson.

Page1 / 27

Lecture6 - Interactive Programs It is often useful to...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online