Introduction
Often while coding, we want command line input from user in interactive way. Then we use the input to execute the program in certain way. But how do we do it? There are several way of presenting choices to users in interactive manner. In this tutorial we learn to read interactive command line input with JAVA Scanner class. We can also use Console class introduced in JAVA 6. However, in this tutorial, we will focus on Scanner class.
What is JAVA Scanner Class?
Scanner class was introduced as part of JAVA 5. This is one good way of addressing this problem of interactive user input from command line. As mentioned in the JAVA doc, it is — “A simple text scanner which can parse primitive types and strings using regular expressions.” However, it can also be doubled up to scan the user input from console in an interactive manner. Next, we’ll demonstrate the same using a simple demo code.
JAVA interactive command line input example
Here, we will write a small JAVA utility. Let’s consider that there are 2 jobs that this utility can accomplish –
- Job number 1 (When user chooses 1 as the option)
- Job number 2 (When user chooses 2 as the option)
If user presses 3, the program gracefully exits. If user makes any other choice, the utility smartly tells the user to make the write choice. Now, let’s look at the program below. I have documented the explanation in the program itself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.iteritory.javatutorial; import java.util.Scanner; public class JAVACommandLineInputWithScanner { public static void main (String[] args){ // declare a variable that will store the user input String userInput; //declate a scanner object to read the command line input by user Scanner sn = new Scanner(System.in); //loop the utility in loop until the user makes the choice to exit while(true){ //Print the options for the user to choose from System.out.println("*****Available Options*****"); System.out.println("*. Press 1 for job number 1"); System.out.println("*. Press 2 for job number 2"); System.out.println("*. Press 3 to exit"); // Prompt the use to make a choice System.out.println("Enter your choice:"); //Capture the user input in scanner object and store it in a pre decalred variable userInput = sn.next(); //Check the user input switch(userInput){ case "1": //do the job number 1 System.out.println("done with job number 1"); break; case "2": //do the job number 2 System.out.println("done with job number 2"); break; case "3": //exit from the program System.out.println("Exiting..."); System.exit(0); default: //inform user in case of invalid choice. System.out.println("Invalid choice. Read the options carefully..."); } } } } |
Run the program and see the result
Now, we will compile and run the program from command line. Let’s see what command line interaction looks like –
Conclusion
In this lite blog, we learnt to use JAVA Scanner class to read user input from command line in an interactive way. In the next one, I will demonstrate the same functionality using JAVA Console class. See you!! 🙂