How to take input from user in java

The simplest and the easiest way to read the user input in java is by using the Java scanner class.

What is scanner class?

java.util.Scanner is a simple text scanner that can parse primitive types and strings using regular expressions.

The input to Scanner class is broken into tokens using a delimiter pattern, which by default matches whitespace.  The resulting tokens may then be converted into values of different types using the various next methods.

Examples

Examples of Scanner class method usage

Here are the examples of various next methods.  In all the examples we will use hasNext*() like hasNextInt() methods to get the user input and next*() method  like nextInt() to read the entered token. This hasNext method is used for pre-validation.

  • Scan Boolean

In the below example we will read a boolean value. The system would prompt a user to enter either true or false.  You would notice that I am using System.out.print so that the user would be  prompted to enter the value in the same line as the message. The next line scanner.hasNextBoolean() takes the user input and validates if it is boolean or not. And if   hasNextBoolean returns true and the user has input true or false then the scanner.nextBoolean() reads the user input to scannedBoolean variable and next line prints it.  If the user has not entered a boolean value then hasNextBoolean returns false so the else block is executed and the scanner.nextLine() would show what was entered by the user. All other methods below follow the same pattern, so i am explaining it only here.

	private static void scanBoolean() {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Please enter either true or false: ");
		if (scanner.hasNextBoolean()) {
			boolean scannedBoolean = scanner.nextBoolean();
			System.out.println("Your input has been evaluated to: " + scannedBoolean);
		} else {
			System.out.println("You input is not boolean,Your input has been evaluated to: " + scanner.nextLine());
		}
		scanner.close();
	}
  • Scan Double
 private static void scanDouble() {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Please enter a double value: ");
		if (scanner.hasNextDouble()) {
			double scannedDouble = scanner.nextDouble();
			System.out.println("Your input has been evaluated to double value: " + scannedDouble);
		} else {
			System.out.println("You input is not double,Your input has been evaluated to: " + scanner.nextLine());
		}
		scanner.close();
	}

 

  • Scan Float

Float range can be found here.

 private static void scanFloat() {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Please enter a float value: ");
		if (scanner.hasNextShort()) {
			short scannedFloat = scanner.nextShort();
			System.out.println("Your input has been evaluated to float value: " + scannedFloat);
		} else {
			System.out.println("You input is not float,Your input has been evaluated to: " + scanner.nextLine());
		}
		scanner.close();
	}

 

  • Scan Integer
 private static void scanInt() {
		Scanner scanner = new Scanner(System.in);
	    System.out.print("Please enter an int value: ");
    	if (scanner.hasNextInt()) {
    		int scannedInteger = scanner.nextInt();
    		System.out.println("Your input has been evaluated to: " + scannedInteger);
    	} else {
			System.out.println("You input is not an integer value,Your input has been evaluated to: " + scanner.nextLine());
		}
    	scanner.close();
	}

 

  • Scan Line
private static void scanLine() {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Please enter a line, enter/return  will be end of line");
		if (scanner.hasNextLine()) {
			String scannedLine = scanner.nextLine();
			System.out.println("Your input has been evaluated to line: " + scannedLine);
		} else {
			System.out.println("You input is not a line,Your input has been evaluated to: " + scanner.nextLine());
		}
		scanner.close();
	}
  • Scan Long
 private static void scanLong() {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Please enter a long value. The signed long has a minimum value of -263 and a maximum value of 263-1");
		if (scanner.hasNextLong()) {
			long scannedLong = scanner.nextLong();
			System.out.println("Your input has been evaluated to long value: " + scannedLong);
		} else {
			System.out.println("You input is not long,Your input has been evaluated to: " + scanner.nextLine());
		}
		scanner.close();
	}

 

  • Scan Short
private static void scanShort() {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Please enter a short value which has a minimum value of -32,768 and a maximum value of 32,767");
		if (scanner.hasNextShort()) {
			short scannedShort = scanner.nextShort();
			System.out.println("Your input has been evaluated to short value: " + scannedShort);
		} else {
			System.out.println("You input is not short,Your input has been evaluated to: " + scanner.nextLine());
		}
		scanner.close();
	}

 

 Example of Scanner class usage

package com.programtalk.beginner.tutorial;

import java.util.Scanner;

public class ScannerExampleComplete {

	static Scanner scanner = new Scanner(System.in);
	public static void main(String[] args) {
		scanLine();
		scanShort();
		scanFloat();
		scanLong();
		scanInt();
		scanBoolean();
		scanDouble();
		scanner.close();
		
	}
	
	private static void scanLine() {
		System.out.print("Please enter a line, enter/return  will be end of line: ");
		if (scanner.hasNextLine()) {
			String scannedLine = scanner.nextLine();
			System.out.println("Your input has been evaluated to line: " + scannedLine);
		} else {
			System.out.println("You input is not a line,Your input has been evaluated to: " + scanner.nextLine());
		}
	}
	
	private static void scanLong() {
		System.out.print("Please enter a long value. The signed long has a minimum value of -263 and a maximum value of 263-1: ");
		if (scanner.hasNextLong()) {
			long scannedLong = scanner.nextLong();
			System.out.println("Your input has been evaluated to long value: " + scannedLong);
		} else {
			System.out.println("You input is not long,Your input has been evaluated to: " + scanner.nextLine());
		}
	}
	
	private static void scanDouble() {
		System.out.print("Please enter a double value: ");
		if (scanner.hasNextDouble()) {
			double scannedDouble = scanner.nextDouble();
			System.out.println("Your input has been evaluated to double value: " + scannedDouble);
		} else {
			System.out.println("You input is not double,Your input has been evaluated to: " + scanner.nextLine());
		}
	}
	
	private static void scanFloat() {
		System.out.print("Please enter a float value: ");
		if (scanner.hasNextShort()) {
			short scannedFloat = scanner.nextShort();
			System.out.println("Your input has been evaluated to float value: " + scannedFloat);
		} else {
			System.out.println("You input is not float,Your input has been evaluated to: " + scanner.nextLine());
		}
	}
	
	private static void scanShort() {
		System.out.print("Please enter a short value which has a minimum value of -32,768 and a maximum value of 32,767: ");
		if (scanner.hasNextShort()) {
			short scannedShort = scanner.nextShort();
			System.out.println("Your input has been evaluated to short value: " + scannedShort);
		} else {
			System.out.println("You input is not short,Your input has been evaluated to: " + scanner.nextLine());
		}
	}

	private static void scanBoolean() {
		System.out.print("Please enter either true or false: ");
		if (scanner.hasNextBoolean()) {
			boolean scannedBoolean = scanner.nextBoolean();
			System.out.println("Your input has been evaluated to: " + scannedBoolean);
		} else {
			System.out.println("You input is not boolean,Your input has been evaluated to: " + scanner.nextLine());
		}
	}
	
	private static void scanInt() {
	    System.out.print("Please enter an int value: ");
    	if (scanner.hasNextInt()) {
    		int scannedInteger = scanner.nextInt();
    		System.out.println("Your input has been evaluated to: " + scannedInteger);
    	} else {
			System.out.println("You input is not an integer value,Your input has been evaluated to: " + scanner.nextLine());
		}
	}
	
}

And here is the output of executing the above program

Please enter a line, enter/return  will be end of line: this is a line
Your input has been evaluated to line: this is a line
Please enter a short value which has a minimum value of -32,768 and a maximum value of 32,767: 21
Your input has been evaluated to short value: 21
Please enter a float value: 2000
Your input has been evaluated to float value: 2000
Please enter a long value. The signed long has a minimum value of -263 and a maximum value of 263-1: 432
Your input has been evaluated to long value: 432
Please enter an int value: 43
Your input has been evaluated to: 43
Please enter either true or false: true
Your input has been evaluated to: true
Please enter a double value: 6781
Your input has been evaluated to double value: 6781.0
Example to add two entered numbers

A very simple example to add two numbers entered by user. We simply rely on the user to provide the correct inputs.

package com.programtalk.beginner.tutorial;

import java.util.Scanner;

public class AddUserInputNumbers {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
	    System.out.print("Please enter first number: ");
	    int firstNumber = scanner.nextInt();
	    System.out.print("Please enter second number: ");
	    int secondNumber = scanner.nextInt();
	    int sum = firstNumber + secondNumber;
	    System.out.println("Sum of the two numbers is " + sum);
	    scanner.close();
	}
}

Common Issues when user input is recieved with Scanner class

In the above AddUserInputNumbers example, if I pass in a String instead of integer the output would be as below

Please enter first number: www
Exception in thread "main" java.util.InputMismatchException
 at java.util.Scanner.throwFor(Unknown Source)
 at java.util.Scanner.next(Unknown Source)
 at java.util.Scanner.nextInt(Unknown Source)
 at java.util.Scanner.nextInt(Unknown Source)
 at com.programtalk.beginner.tutorial.AddUserInputNumbers.main(AddUserInputNumbers.java:10)

InputMismatchException occurred because i passed in string to a nextInt() method. That is why in all my examples  above I have used hasNextInt() before calling nextInt().

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.