Module 3: Gathering User Input Flashcards
Scanner class
It is often useful to design interactive programs that read input data from the user.
The Scanner class provides convenient methods for reading input values of various types.
A scanner object can be set up to read input values from various sources, including the user typing values on the keyboard.
How does scanner get user input?
You create a scanner object:
Scanner scan = new Scanner(System.in); Once created, the scanner object can be used to invoke various input method, such as:
String answer = scan.nextLine();
The nextLine method reads all of the input until the end of the line is found, and return it as one String.
Scanner methods
nextInt();
nextDouble();
nextFloat();
nextLong();
nextByte();
nextShort();
next();
nextLine();
Do you need to import Scanner?
Yes, it is a Class located in the Java.util package so must be imported to be used.