
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Input from the User in Java
In this article, we will understand how to get input from users in Java. This is achieved using a scanner object. The Scanner.nextInt() method is used to get the input. The java.util.Scanner.nextInt() method Scans the next token of the input as an int. An invocation of this method of the form nextInt() behaves in the same way as the invocation nextInt(radix), where radix is the default radix of this scanner.
Problem Statement
Write a program in Java to get input from the user. Below is a demonstration of the same ?
Input
Hello, I am John!
Output
The input string is: Hello, I am John!
Approaches to get input from the user
Below are the different approaches to get input from the user ?
Using Scanner class
Following are the steps to get input from users in Java using the Scanner class ?
- First import the Scanner class from java.util package.
- Instantiate the Scanner object to read input from the console.
- Notify the user and prompt input.
- Use scanner.nextLine() to capture the entire line of text entered by the user and store it in a variable named value.
- Print the message followed by the user input stored in value.
Example
Here, the input is entered by the user based on a prompt. You can try this example live in our coding ground tool.
import java.util.Scanner; public class PrintString{ public static void main(String[] args){ String value; Scanner scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.print("Enter a string: "); value = scanner.nextLine(); System.out.println("The nextLine method is used to read the string value "); System.out.println("The string is: "); System.out.println(value); } }t
Output
A reader object has been defined Enter a string: Good Morning! The nextLine method is used to read the string value The string is: Good Morning!
Using InputStreamReader and BufferReader
Following are the steps to get input from users in Java using InputStreamReader and BufferReader ?
- Import all the classes from the java.io package
- Create InputStreamReader object to read data from the console.
- Informs the user that the InputStreamReader object has been successfully created by printing creation message.
- Wraps the InputStreamReader in a BufferedReader for efficient reading.
- Tell the user that the BufferedReader object is now ready by printing the constructor message.
- Prompt user for input.
-
Integer.parseInt(in.readLine()) reads the input as a string, converts it to an integer, and stores it in the number variable.
Example
Here, the input is being entered by the user based on a prompt using an InputStreamReader object. You can try this example live in our coding ground tool.
import java.io.*; public class readNum{ public static void main(String args[]) throws IOException{ InputStreamReader read=new InputStreamReader(System.in); System.out.println("An object of InputStreamReader class is created"); BufferedReader in=new BufferedReader(read); System.out.println("A constructor of the BufferedReader class is created"); System.out.println("Enter a number: "); int number=Integer.parseInt(in.readLine()); } }
Output
An object of InputStreamReader class is created A constructor of the BufferedReader class is created Enter a number: 34