Example to read the user input using Scanner Class





Hey guys in this, we will discuss accepting the user input in Java with Scanner class. If we are creating a console-based application, it’s essential that takes the user input and produces the correct output. So in this post let’s see how we can take the user input using the Scanner class.

Overview


The Scanner class is used to get the user input, and it is found in java.util package.

To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class. There are several methods available in Scanner class for reading different data types.

Methods to read different datatypes


  • nextBoolean(): As the name suggests, it is used to read the boolean value from the user
  • nextByte(): As the name suggests, it is used to read the byte value from the user
  • nextDouble(): As the name suggests, it is used to read the double value from the user
  • nextFloat(): As the name suggests, it used to read the float value from the user
  • nextInt(): As the name suggests, it used to read the int value from the user
  • nextLine(): As the name suggests, it is used to read the String value from the user.
  • nextLong(): As the name suggests, it is used to read the long value from the user.
  • nextShort(): As the name suggests, it is used to read the short value from the user.

Of course, we cannot discuss all of the methods in detail because that will take a lot of time. Let’s look at the example for taking user input for String, int, and double.

Usage of nextLine()


As we discussed nextLine() is used to read the String value from the user


import java.util.Scanner;

public class UserInput {
	
	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		System.out.println("Enter your name:");
		String name = scanner.nextLine();
		
		System.out.println("My name is "+name+".");
		
	}
}

Output:

Enter your name:
Bushan Sirgur
My name is Bushan Sirgur.

Usage of nextInt() and nextDouble()


As we discussed nextInt() and nextDouble() is used to read the integer and double value respectively.

import java.util.Scanner;

public class UserInput {
	
	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		System.out.println("Enter your name:");
		String name = scanner.nextLine();
		
		System.out.println("Enter your age:");
		int age = scanner.nextInt();
		
		System.out.println("Enter your salary:");
		double salary = scanner.nextDouble();
		
		
		System.out.println("My name is "+name+". I am "+age+" years old. I am earning "+salary+"/-");
		
	}
}

Output:

Enter your name:
Bushan
Enter your age:
28
Enter your salary:
10000
My name is Bushan. I am 28 years old. I am earning 10000.0/-




About the author

Bushan Sirgur

Well, I am Bushan Sirgur from Banglore, India. Currently, I am working as a Software Developer in a Service Base Company. I am interested in JAVA/J2EE, Angular 2, JavaScript, jQuery, MongoDB.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *