In addition to the Math and String classes we learned about earlier, Java has many more classes we can access. But, unlike the Math and String classes, because they are not included with the core, they must be imported.
The class we are going to learn about today is the Scanner class. The Scanner class allows us to get input from the user whether it be from a file or the keyboard. The following code illustrates how to import the libraries required to get user input using the Scanner class.
import java.io.*;
import java.util.*;
public class ScannerPractice{
}
With the above libraries imported, we can access methods that will make our Java programs much more interactive!
Before we can access the methods available to us, we must create a Scanner object. You can call the Scanner object anything you like, so long as it follows the Java variable naming conventions. The code below creates a Scanner object called kbReader. What each part of the code represents is broken down below,
Scanner kbReader = new Scanner(System.in);
The kbReader object we created above will allow us to get user input. This process is illustrated below.
User input of type intGetting int data types from the user requires the nextInt() method. Consider the following code
Scanner kbReader = new Scanner(System.in);
System.out.println("How old are you?"):
int age = kbReader.nextInt();
System.out.println("You were born in " + (2019 - age));
In the above code, the line int age = kbReader.nextInt()
is doing two things: (1)
it is prompting the user for an integer input from the keyboard and (2) it is assigning the user input
to the int type variable age
Also notice the dot notation. We have used this before. The dot notation allows us to access the Scanner libraries we imported previously.
User input of type doubleGetting double data types from the user requires the nextDouble() method. Consider the following code
Scanner kbReader = new Scanner(System.in);
System.out.println("How tall are you in inches to the nearest 10th decimal place?"):
int height = kbReader.nextDouble();
System.out.println("You are " + (int)(height/12) + " feet " + (height % 12) + " inches" );
In the above code, the line double height = kbReader.nextDouble()
is doing two things: (1)
it is prompting the user for a double input from the keyboard and (2) it is assigning the user input
to the double type variable height
The next() method accepts String input up to the first white space. For example if I type Computer Science as input, the next() method only scans the word "Computer". This is illustrated below.
Scanner kbReader = new Scanner(System.in);
System.out.println("What is the name of this class?"):
String firstWord = kbReader.next();//Suppose the user types Computer Science
System.out.println(firstWord);//Only the word Computer is printed
The nextLine() method can be used to retrieve an entire line, including white space. This is illustrated below.
Scanner kbReader = new Scanner(System.in);
System.out.println("What is the name of this class?"):
String myClass = kbReader.nextLine();//Suppose the user types Computer Science
System.out.println(myClass);//Computer Science is printed
Using a single Scanner object, the methods nextInt(), nextDouble, next(), and nextLine() may be used in any sequence with the following exception:
It is not permissible to follow nextInt() or nextDouble() with nextLine(). If it is necessary to do this, then a new Scanner object must be constructed for use with nextLine() and any subsequent inputs.