Scanner




The Scanner class allows you to read in data from a file or from the keyboard. You must
import java.util.Scanner; (or import java.util.*;) You can also pass the data to the Scanner
object as a String. You can remove elements from the Scanner object by calling one of the
next methods (this will return the data and remove it from the Scanner object).



Scanner Methods

next() returns the next element in the Scanner object as a String (reads up to white space) (i.e. space, tab, or new line character) nextLine() returns the next line in the Scanner object as a String (discards the new line character) nextByte() returns the next element in the Scanner object as a byte nextShort() returns the next element in the Scanner object as a short nextInt() returns the next element in the Scanner object as an int nextLong() returns the next element in the Scanner object as a long nextFloat() returns the next element in the Scanner object as a float nextDouble() returns the next element in the Scanner object as a double hasNext() returns true if there is another element in the Scanner object hasNextLine() returns true if there is another line in the Scanner object hasNextByte() returns true if the next element in the Scanner object is a byte hasNextShort() returns true if the next element in the Scanner object is a short hasNextInt() returns true if the next element in the Scanner object is an int hasNextLong() returns true if the next element in the Scanner object is a long hasNextFloat() returns true if the next element in the Scanner object is a float hasNextDouble() returns true if the next element in the Scanner object is a double
Examples:

import java.util.*; class Main { public static void main(String[] args) { // Creates a Scanner object. // You can get input from the user. // The variable kb will refer to the object. // The variable can be called kb, // or keyboard, or scan, or ? // Variables should always start with a // lower case letter, and then you can use // more letters or digits or an _ Scanner kb = new Scanner(System.in); System.out.println("Area of a Triangle"); System.out.println(); System.out.println(); // We prompt the user to enter the width // NOTE: we use print, NOT println() System.out.print("Enter the width: "); // The user should enter the width // We will convert the input to an int int width = kb.nextInt(); kb.nextLine(); // flush the buffer // We prompt the user to enter the height. // NOTE: we use print, NOT println() System.out.print("Enter the height: "); // The user should enter the height. // We will convert the input to an int int height = kb.nextInt(); kb.nextLine(); // flush the buffer // Here we calculate the area and store // the answer in the variable area. int area = width * height; // Skip a line before printing output System.out.println(); // Finally, we print out the result. System.out.println("The area is " + area + " Square Units."); } }
Scanner scan = new Scanner(System.in); // reads from the keyboard System.out.print("Enter your grade "); int grade = scan.nextInt(); // stops and waits for the user to enter an integer number and press enter System.out.println(grade); // prints out the number that the user entered
Scanner scan = new Scanner("12 5 7"); scan.nextInt() // returns 12 as an int (and removes the 12 from the list) out.println(scan.nextInt()); // prints out 5 (and removes the 5 from the list) out.println(scan.nextInt()); // prints out 7 (and removes the 7 from the list)
Scanner scan = new Scanner("abc 5 xyz"); out.println(scan.next()); // prints out abc out.println(scan.nextInt()); // prints out 5 out.println(scan.next()); // prints out xyz
Scanner scan = new Scanner("12 xyz 7"); out.println(scan.nextInt()); // prints out 12 if (scan.hasNextInt()) out.println(scan.nextInt()); // this is skipped else out.println(scan.next()); // prints out xyz out.println(scan.nextInt()); // prints out 7
Scanner scan = new Scanner("12 5 7"); int sum = 0; while (scan.hasNextInt()) { int value = scan.nextInt(); sum = sum + value; System.out.print(value+" "); // prints out 12 5 7 } System.out.println("The sum is " + sum); // prints out 24
// reads in the data from the file MyFile.txt // suppose 12 5 7 is in the file // you must include import java.io.*; at the top of your program // and you must include a try block or use a throws statement try { Scanner scan = new Scanner(new File("MyFile.txt")); int sum = 0; while (scan.hasNextInt()) { int value = scan.nextInt(); sum = sum + value; System.out.print(value+" "); // prints out 12 5 7 } System.out.println("The sum is " + sum); // prints out 24 } catch (Exception e) { System.out.println("There was an error reading the file"); }