• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer
  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc
Tech Stack Journal

Tech Stack Journal

How to read input from User in Java

April 21, 2020 by Admin Leave a Comment

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());

	}

}

Filed Under: Java

Previous Post: « How to add a List to another List in Java
Next Post: Bubble Sort in Java »

Reader Interactions

Leave a Reply Cancel reply

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


Primary Sidebar

More to See

Arrays.asList in Java Examples

February 21, 2021 By Admin

[Solved] Why List.add throws UnsupportedOperationException in Java?

February 20, 2021 By Admin

Secondary Sidebar

Categories

  • Algorithms
  • Blogging
  • Docker
  • Java
  • Misc
  • Node.js
  • Spring Core
  • Windows

Archives

  • February 2021 (6)
  • January 2021 (1)
  • December 2020 (1)
  • September 2020 (2)
  • August 2020 (5)
  • July 2020 (4)
  • June 2020 (1)
  • May 2020 (4)
  • April 2020 (22)
  • November 2019 (3)
  • September 2019 (2)
  • August 2019 (6)

Footer

Navigation

  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc

Recent

  • How to Make File Explorer Open to This PC instead of Quick Access in Windows 10
  • Arrays.asList in Java Examples
  • [Solved] Why List.add throws UnsupportedOperationException in Java?
  • How to Convert an Array to List in Java?
  • How Many Spaces in a Tab?

Search

Copyright © 2021 · Tech Stack Journal · Log in