How to read input from User in Java

In this post, we will discuss on how to read input from User using Scanner and InputStreamReader.

Scanner(System.in)

To read values of known type from standard input, we can use Scanner class. When I say known types, any primitive like int, double, char or a String.

Scanner provides various utility methods to read input such as nextShort(), nextFloat(), nextDouble(), nextByte(), nextLong() etc., You can also read String using nextLine() and next() functions.

Since, we want to read input from standard input stream, System.in needs to be passed as an argument to Scanner class constructor while instantiating it.

package com.techstackjournal;

import java.io.IOException;
import java.util.Scanner;

public class ReadInput {

	public static void main(String[] args) throws IOException {

		Scanner scanner = new Scanner(System.in);

		System.out.print("Enter an integer value:");
		int num1 = scanner.nextInt();
		scanner.nextLine();

		System.out.print("Enter a float value:");
		float num2 = scanner.nextFloat();
		scanner.nextLine();

		System.out.print("Enter a double value:");
		double num3 = scanner.nextDouble();
		scanner.nextLine();

		System.out.print("Enter a string:");
		String str1 = scanner.nextLine();

		System.out.print("Enter a string:");
		String str2 = scanner.next();

		scanner.close();

		System.out.println("Entered integer: " + num1);
		System.out.println("Entered float: " + num2);
		System.out.println("Entered double: " + num3);
		System.out.println("Entered string: " + str1);
		System.out.println("Entered string: " + str2);

	}

}

Methods nextInt, nextFloat(), nextDouble() read only their respective number but leave the new line character. So we need to explicitly call nextLine() method to consume the new line character after reading those numbers.

The difference between nextLine() and next() is that nextLine() read the line till we press new line character, whereas next() read the characters till it finds a space character.

After reading the input is complete we need to close the Scanner by calling its close() function.

BufferedReader(InputStreamReader(System.in))

  • To read input from User using BufferedReader, we need to pass an instance of InputStreamReader which in turn is instantiated using System.in.
  • Once BufferedReader is instantiated, we can use readLine() function to read the text from standard input.
  • Again, after reading the input we need to close the BufferedReader by calling its close() function.
package com.techstackjournal;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BufferedReaderExample {

	public static void main(String[] args) throws IOException {

		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

		System.out.print("Enter your name:");
		String yourName = reader.readLine();
		reader.close();

		System.out.println("Your name in Uppercase: " + yourName.toUpperCase());

	}

}

Leave a Comment