
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Get Input from the User
In this article, we will understand how to get an input from user in Java. This 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 exactly the same way as the invocation nextInt(radix), where radix is the default radix of this scanner.
Below is a demonstration of the same −
Input
Suppose our input is −
Hello, I am John!
Output
The desired output would be −
The input string is: Hello, I am John!
Algorithm
Step1- Start Step 2- Declare a string: value Step 3- Prompt the user to enter a string Step 4- Read the values Step 5- Display the value Step 6- Stop
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding 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); } }
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!
Example 2
Here, the input is being entered by the user based on a prompt using a InputStreamReader object.
Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding 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
- Related Articles
- Swift Program to Get Input from the User
- Haskell program to get input from the user
- How to create input Pop-Ups (Dialog) and get input from user in Java?
- Java Program to Print Swastika By Taking Input from User
- How to get Input from the User in Golang?
- Java Program to fill an array of characters from user input
- Python Get a list as input from user
- Get number from user input and display in console with JavaScript
- Taking input from the user in Tkinter
- How to input multiple values from user in one line in Java?
- Can we read from JOptionPane by requesting input from user in Java?
- Java Program to Read The Number From Standard Input
- Take Matrix input from user in Python
- Program to find out the data type of user input in C++
- Java program to accept an integer from user and print it
