Java User Input
In this example tutorial, we will see how to get user input from keyboard in java. In java, we use Scanner class that is located in java.util package to get the user input. First of all, we have to import the Scanner class in our java class file. Such as,
//import the Scanner class
import java.util.Scanner;
public class JavaScannerClassExample1 {
//main method
}
Now create an object of Scanner class.
import java.util.Scanner;
public class JavaScannerClassExample1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
}
}
Now Scanner class contains specific methods to read user input either string, int or float. Such as,
nextLine(): call it in order to read string or text values
nextInt(): call it in order to read int values
nextLong(): call it in order to read long value
nextFloat(): call it in order to read decimal number.
nextDouble(): call it in order to read double value.
next().charAt(index_num): call it in order to read characters.
next().Boolean(): call it in order to read boolean (true/false) value
nextByte(): call it in order to read byte value (number)
nextShort(): call it in order to read short value (number)
Let's look over the following examples of few of them.
Read Int Value
Int value means, numbers either positive or negative without decimal point. In order to read int value of the user input, we have to use nextInt() method. Such as,
import java.util.Scanner;
public class JavaScannerClassExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Your First Number");
int num1 = sc.nextInt();
System.out.println("You entered: "+num1);
}
}
/**
* Enter Your First Number
* 10
* You Entered 10
*/
Read Text or String Values
In order to read text or String values (sequence of characters) then, we will use nextLine() method of Scanner class.
import java.util.Scanner;
public class JavaScannerClassExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name, role, companyName, city;
System.out.print("Enter Your Name: ");
name = sc.nextLine();
System.out.print("Enter Your Role: ");
role = sc.nextLine();
System.out.print("Enter Your companyName: ");
companyName = sc.nextLine();
System.out.print("Enter Your City: ");
city = sc.nextLine();
System.out.println("Your Info");
System.out.printf("Name= %s; role= %s; companyName= %s; city= %s", name, role, companyName, city);
}
}
import java.util.Scanner;
public class JavaScannerClassExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name, role, companyName, city;
System.out.print("Enter Your Name: ");
name = sc.nextLine();
System.out.print("Enter Your Role: ");
role = sc.nextLine();
System.out.print("Enter Your companyName: ");
companyName = sc.nextLine();
System.out.print("Enter Your City: ");
city = sc.nextLine();
System.out.println("Your Info");
System.out.printf("Name= %s; role= %s; companyName= %s; city= %s", name, role, companyName, city);
}
}
/**
* Enter Your Name: Shakil Ahmed
* Enter Your Role: Java Developer
* Enter Your companyName: TheXLR
* Enter Your City: Dhaka
* Your Info
* Name= Shakil Ahmed; role= Java Developer; companyName= TheXLR; city= Dhaka
*/
Read Floating Point Numbers
We can use nextFloat() to read floating point numbers as well. Such as,
import java.util.Scanner;
public class JavaScannerClassExample {
public static void getArea(){
Scanner sc = new Scanner(System.in);
float length, width, area = 0.0f;
System.out.print("Enter Length: ");
length = sc.nextFloat();
System.out.print("Enter Width: ");
width = sc.nextFloat();
area = length * width;
System.out.println("Area of Rectangle: "+ area);
}
public static void main(String[] args){
//call the getArea() method
getArea(); //as it is static, so don't need to create object
}
}
/**
* Enter Length: 12.5
* Enter Width: 8.5
* Area of Rectangle: 106.25
*/