
- 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
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 Articles
- Python program to read input from console
- Way to read input from console in C#
- Read integers from console in Java
- How to read data from user using the Console class in Java?
- Taking input from console in Python
- How to read data from PDF file and display on console in Java?
- Java Program to Read The Number From Standard Input
- How can we read from standard input in Java?
- How to read a line from the console in C#?
- Can we read from JOptionPane by requesting input from user in Java?
- How many ways can we read data from the keyboard in Java?
- Haskell program to read numbers from standard input
- Java Program to read the next byte of data from the input stream
- How to read an input value from a JTextField and add to a JList in Java?
- Get number from user input and display in console with JavaScript

Advertisements