

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Ways to read input from console in Java
Let us see some ways to read input from console in Java −
Example
import java.util.Scanner; public class Demo{ public static void main(String args[]){ Scanner my_scan = new Scanner(System.in); String my_str = my_scan.nextLine(); System.out.println("The string is "+my_str); int my_val = my_scan.nextInt(); System.out.println("The integer is "+my_val); float my_float = my_scan.nextFloat(); System.out.println("The float value is "+my_float); } }
Output
The string is Joe The integer is 56 The float value is 78.99
A class named Demo contains the main function. An instance of the Scanner class is created and the ‘nextLine’ function is used to read every line of a string input. An integer value is defined and it is read from the standard input console using ‘nextInt’. Similarly, ‘nextFloat’ function is used to read float type input from the standard input console. They are displayed on the console.
Example
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Demo{ public static void main(String[] args) throws IOException{ BufferedReader my_reader = new BufferedReader(new InputStreamReader(System.in)); String my_name = my_reader.readLine(); System.out.println("The name is "); System.out.println(my_name); } }
Output
The name is Joe
A class named Demo contains the main function. Here, an instance of the buffered reader is created. A string type of data is defined and every line of the string is read using the ‘readLine’ function. The input is given from the standard input, and relevant message is displayed on the console.
- Related Questions & Answers
- Python program to read input from console
- Way to read input from console in C#
- Read integers from console in Java
- Taking input from console in Python
- How to read data from user using the Console class in Java?
- Java Program to Read The Number From Standard Input
- How to read data from PDF file and display on console in Java?
- How to read a line from the console in C#?
- How can we read from standard input in Java?
- Can we read from JOptionPane by requesting input from user in Java?
- How many ways can we read data from the keyboard in Java?
- Java Program to read the next byte of data from the input stream
- Get number from user input and display in console with JavaScript
- How to read an input value from a JTextField and add to a JList in Java?
- Java program to read numbers from users
Advertisements