How To Take Input From Users In Java?

By | June 25, 2023

how to take inputs from users in Java

Java is the most commonly used programming language in today’s business world, which plays an important role. A flexible and secure platform for developing a broad range of applications, from Web apps to enterprise software systems, allows you to create unlimited applications. A number of companies use Java to ensure that their applications are compatible with any operating system or device across various platforms.

In addition, Java’s robustness and scale make it an excellent tool for handling large business operations and complex data processing tasks. It further accelerates its development, enabling enterprises to deliver efficient and reliable software solutions to meet their specific needs by offering a large library of prebuilt code and frameworks. Java course remains a popular choice for businesses since it is reliable, flexible, and powerful in empowering digital transformation initiatives.

Using The Scanner Class,

It’s a slightly upgraded version of BufferedReader. The scanner can read the format to take input from the user in Java. There are various functions available for different types of data. 

  • It’s easier to read the scanner because we don’t have to write the throws because there’s no exception. 
  • In later versions of Java, they were added. 
  • You’ll also find predefined functions for reading an Intgram, a Character and another data type.

Method of Java Scanner Class

Basic Syntax: Scanner scn = new Scanner(System.in);

Importing scanner class:  The scanner class must be imported from the util package so that it can be used

import java.util.Scanner;

Inbuilt Scanner functions are as follows:

Integer: nextInt()

Float: nextFloat()

String : next() and nextLine().

Therefore, we do not need to parse Integer and String in Scanner as we did in BufferedReader.

Method Description
int nextInt() It is used to scan the next token of the input as an integer.
float nextFloat() It is used to scan the next token of the input as a float.
double nextDouble() It is used to scan the next token of the input as a double.
byte nextByte() It is used to scan the next token of the input as a byte.
String nextLine() Advances this scanner past the current line.
boolean nextBoolean() It is used to scan the next token of the input into a boolean value.
long nextLong() It is used to scan the next token of the input as a long.
short nextShort() It is used to scan the next token of the input as a Short.
BigInteger nextBigInteger() It is used to scan the next token of the input as a BigInteger.
BigDecimal nextBigDecimal() It is used to scan the next token of the input as a BigDecimal.

Example:

 

import java.util.Scanner;

public class UserInputExample {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print(“Enter an integer: “);

        int number = scanner.nextInt();

        System.out.println(“You entered: ” + number);

        scanner.close();

    }

}

In this code snippet, we’ll create a scanner object by using System.ins as an argument that represents the standard input stream. Next, we’ll write an integer to a user using the nextInt() method. You should always call scanning.close() when you’ve finished using the Scanner object to open related resources.

Using The BufferedReader Class,

It’s a simple class for reading the sequence of characters. It’s got a little function that reads characters, and another is reading an array of characters, and a readLine() function that reads a line. The InputStreamReader function converts the input bytes into a stream of characters to read as BufferedReader expects a stream of characters.

Method of BufferedReader Class

Method Description
int read() It is used for reading a single character.
int read(char[] cbuf, int off, int len) It is used for reading characters into a portion of an array.
boolean markSupported() It tests the input stream support for the mark and reset method.
String readLine() It is used for reading a line of text.
boolean ready() It tests whether the input stream is ready to be read.
long skip(long n) It is used for skipping the characters.
void reset() It repositions the stream at a position the mark method was last called on this input stream.
void mark(int readAheadLimit) It is used for marking the present position in a stream.
void close() It closes the input stream and releases any system resources associated with it.

Example:

// Java Program for taking user

// input using BufferedReader Class

import java.io.*;

class GFG {

// Main Method

public static void main(String[] args)

throws IOException

{

// Creating BufferedReader Object

// InputStreamReader converts bytes to

// stream of character

BufferedReader bfn = new BufferedReader(

new InputStreamReader(System.in));

// String reading internally

String str = bfn.readLine();

// Integer reading internally

int it = Integer.parseInt(bfn.readLine());

// Printing String

System.out.println(“Entered String : ” + str);

// Printing Integer

System.out.println(“Entered Integer : ” + it);

}

}

Output:

Neha Soni

121

Entered String: Neha Soni

Entered Integer: 121

Difference between Scanner Class and BufferedReader Class

Sr. No. Key Scanner Class BufferReader Class
1 Synchronous Scanner is not is synchronous in nature and should be used only in a single-threaded case. BufferReader is synchronous in nature. During a multithreading environment, BufferReader should be used.
2 Buffer Memory Scanner has a little buffer of 1 KB char buffer. BufferReader has a large buffer of 8KB byte Buffer as compared to Scanner.
3 Processing Speed Scanner is a bit slower as it needs to parse data as well. BufferReader is faster than Scanner as it only reads a character stream.
4 Methods Scanner has methods like nextInt(), nextShort() etc. BufferReader has methods like parseInt(), parseShort() etc.
5 Read Line Scanner has the method nextLine() to read a line. BufferReader has the method readLine() to read a line.

Frequently Asked Questions

 

Q1. What’s a Java class called BufferedReader? 

Ans.  To read the stream of characters from the specified source (character-input stream), we can use the BufferedReader class of Java. An InputStream object will be accepted as a parameter by the constructor of this class.

Q2. How to take input from users in Java?

Ans.  Use method “nextLine()” which is used to read a string value for the user. 

Q3.  What’s the way to input string values into Java after int? 

Ans. To take input as an integer, we use nextInt() function, which does not read the new line character of your input. So it will take the new line as its new line and give you a new line if we call nextLine().

Telegram Group Join Now
WhatsApp Channel Join Now
YouTube Channel Subscribe
Scroll to Top
close
counselling
Want to Enrol in PW Skills Courses
Connect with our experts to get a free counselling & get all your doubt cleared.