Receiving user data is an important part of coding to interact with the user. To receive input from the user, the Scanner
class is used. This is found under the java.util
package.
Hence we will need to import the library first,
import java.util.Scanner;
Note
This is the most important step and also the easiest to forget. If the Scanner
class is not implemented, all its function and instances cannot be used.
Now we can use the Scanner class by creating an object of the class.
Scanner input = new Scanner(System.in);
The line above creates a Scanner instance input that will read the user input depending on the methods called. The following are the eight most common methods to retrieve user input depending on the data type.
Methods | Return Type |
---|---|
nextByte() |
Byte |
nextShort() |
Short |
nextInt() |
Int |
nextLong() |
Long |
nextFloat() |
Float |
nextDouble() |
Double |
nextLine() |
String |
nextBoolean |
Boolean |
Important
It is important that the input type matches the method's data type, or else you will get an exception/error message.
import java.util.Scanner; // Import Scanner library
class TestClass
{
public static void main(String[] args)
{
// Create an instance of the Scanner class
Scanner input = new Scanner(System.in);
// Source code as follows
}
}
System.out.println("Enter a byte integer:");
// Reading the input as byte data type
byte aByte = input.nextByte();
System.out.println("aByte = " + aByte);
Output
Enter a byte integer:
5
aByte = 5
System.out.println("Enter a short integer:");
// Reading the input as short data type
short aShort = input.nextShort();
System.out.println("aShort = " + aShort);
Output
Enter a short integer:
50
aShort = 50
System.out.println("Enter a integer:");
// Reading the input as a int data type
int aInt = input.nextInt();
System.out.println("aInt = " + aInt);
Output
Enter a integer:
100
aInt = 100
System.out.println("Enter a long integer:");
// Reading the input as a long data type
long aLong = input.nextLong();
System.out.println("aLong = " + aLong);
Output
Enter a long integer:
12345
aLong = 12345
System.out.println("Enter a float:");
// Reading the input as a float data type
float aFloat = input.nextFloat();
System.out.println("aFloat = " + aFloat);
Output
Enter a float:
95.43
aFloat = 95.43
System.out.println("Enter a double:");
// Reading the input as a double data type
double aDouble = input.nextDouble();
System.out.println("aDouble = " + aDouble);
Output
Enter a double:
97584.45
aDouble = 97584.45
System.out.println("Enter a string:");
// Reading the input as a string data type
String aString = input.nextLine();
System.out.println("aString = " + aString);
Output
Enter a string:
Hello World
aString = Hello World
System.out.println("Enter a boolean:");
// Reading the input as a boolean variable
boolean aBoolean = input.nextBoolean();
System.out.println("aBoolean = " + aBoolean);
Output
Enter a boolean:
true
aBoolean = true
The following block of code shows an example of using the Scanner library.
import java.util.Scanner; // Import Scanner library
class TestClass
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Please enter your name: ");
String name = input.nextLine();
System.out.println("Hi " + name + ", what is your favourite number?");
int num = input.nextInt();
System.out.println("Your favourite number is " + num + ".");
}
}
Output
Please enter your name: Jack
Hi Jack, what is your favourite number?
7
Your favourite number is 7.