Thursday, August 19, 2021

How to read input from command line in Java using Scanner - Example

Java 5 introduced a nice utility called java.util.Scanner is capable of reading input from the command line in Java. Using Scanner is a nice and clean way of retrieving user input from the console or command line. The scanner can accept InputStream, Reader, or simply the path of the file from where to read input. In order to read from the command line, we can pass System.in into Scanner's constructor as a source of input. The scanner offers several benefits over the classical BufferedReader approach, here are some of the benefits of using java.util.Scanner for reading input from the command line in Java:

1) The scanner supports regular expression, which gives you the power to read-only matching the pattern from the input.

2) The scanner has methods like nextInt(), nextFloat() which can be used to read numeric input from the command line and can be directly used in code without using Integer.parseInt() or any other parsing logic to convert String to Integer or String to double for floating-point input.

Reading input from the command line should be the first few things which a new Java programmer should be taught as it helps them to write an interactive program and complete programming exercises like checking for a prime number, finding the factorial of a number, reversing String, etc. 

Once you are comfortable reading input from the command line you can write many interactive Java applications without learning in GUI technology like Swing or AWT.


Java program to read input from command prompts in Java

Here is a sample code example of How to read input from the command line or command prompt in Java using java.util.Scanner class:



import java.io.IOException;
import java.util.Scanner;

public class ReadInputScannerExample {

    public static void main(String args[]) throws IOException  {
 
        Scanner scanner = new Scanner(new InputStreamReader(System.in));
        System.out.println("Reading input from console using Scanner in Java ");
        System.out.println("Please enter your input: ");
        String input = scanner.nextLine();
        System.out.println("User Input from console: " + input);
        System.out.println("Reading int from console in Java: ");
        int number = scanner.nextInt();
        System.out.println("Integer input: " + number);
     
    }
 
}

Output:
Reading input from the console using Scanner in Java
Please enter your input:
number
User Input from the console: number
Reading int from the console in Java:
1232
Integer input: 1232

Just note that java.util.Scanner nextInt() will throw "Exception in thread "main" java.util.InputMismatchException" exception if you entered String or character data which is not number while reading input using nextInt(). That’s all on how to read user input from the command line or command prompt in Java.



Related Java programming tutorials for beginners

3 comments :

Unknown said...

how to make program with input from user abab
ouput- a
ab
aba
abab
b
ba
bab
baba
babab

vip said...

can i make
System.out.println("Reading int from console in Java: ");
int number = scanner.nextInt();

the above code in single line, both the lines into 1 single line????

Anonymous said...

Your code is not working in jre1.8.0_191.
It has something to do with InputStreamReader.
Apprently when i removed InputStreamReader from the code and compiled it, it gave no error and worked fine.

Post a Comment