How to get input from users in Java from the command line or console is one of the common thing, everyone started learning Java looks for. It’s the second most popular way of
starting programming after HelloWorld
in Java. There are so many ways to get input from Users in Java including
command line and Graphical user interface. For beginners simpler the example,
better it is. first and foremost way of getting input from user is String[]
passed to main
method in Java but that only work for one time requirement and its not
interactive. Another way of getting input from User is involving IO classes
like InputStream
to read from console or command line which can be little complicated for true
beginner.
Thanks to Java 5 which added a nice utility class called Scanner, has made task of getting input from user very easy. Scanner is powerful and allows you to get any kind of input from User e.g. String, int, float etc.
Thanks to Java 5 which added a nice utility class called Scanner, has made task of getting input from user very easy. Scanner is powerful and allows you to get any kind of input from User e.g. String, int, float etc.
On the other hand if you are looking to use a GUI for getting input from
user than best option is to use JOptionPane. JOptionPane allows you
to show dialog box and get input from user without all the hassles of Swing API,
which is also a popular Java
Swing Interview question.By the way In this Java Program we will see both Scanner and JOptionPane to get
input from User.
How to get Input from User from the command line or Console in Java
Here is complete code example of Java Program to get input from user
interactively. This Java Program uses java.util.Scanner to
get String,
int and float as input from User and then finally display a dialog box using JOptionPane to get
input from User. you can use any approach as per your need.
/**
* Simple Java program to get Input from User. Shows two examples to get input from
* Simple Java program to get Input from User. Shows two examples to get input from
* command line and getting input
using GUI screen by using JOptionPane.
* Scanner class can be used to
get different kind of input from User
* e.g. String, int, char, float
and double.
*
* JOptionPane has static utility method which can display dialog boxes and ask
*
* JOptionPane has static utility method which can display dialog boxes and ask
* user to enter input, a much
interactive way of getting input from User.
* JOptionPane returns user
entered value as String which can then be converted
* to number by using
Integer.parseInt or Float.parseFloat() etc.
*
* @author Javarevisited
*/
public class InputFromUser {
public static void main(String args[]) throws IOException {
//Java Exmaple to get input from user from command prompt
System.out.println("Please enter input from command prompt: ");
Scanner inputReader = new Scanner(System.in);
* @author Javarevisited
*/
public class InputFromUser {
public static void main(String args[]) throws IOException {
//Java Exmaple to get input from user from command prompt
System.out.println("Please enter input from command prompt: ");
Scanner inputReader = new Scanner(System.in);
//Getting input in String format
String name = inputReader.nextLine();
System.out.println("Hi " + name);
//Getting number as input from command line in Java
System.out.println("Please enter a number from command line? ");
int number = inputReader.nextInt();
System.out.println("You have entered : " + number);
//Getting floating point as input from command line in Java
System.out.println("Please enter a floating point number from command line? ");
float decimal = inputReader.nextFloat();
System.out.println("You have entered : " + decimal);
//Java Example to get input from user using GUI
String input = JOptionPane.showInputDialog("Enter any number of your choice");
System.out.println("User has entered: " + input);
}
}
Output:
Please enter input from a command prompt:
Java Programming tutorial
Hi Java Programming tutorial
Please enter a number from the command line?
22
You have entered: 22
Please enter a floating-point number from the command line?
22.34
You have entered: 22.34
User has entered: 34343
String name = inputReader.nextLine();
System.out.println("Hi " + name);
//Getting number as input from command line in Java
System.out.println("Please enter a number from command line? ");
int number = inputReader.nextInt();
System.out.println("You have entered : " + number);
//Getting floating point as input from command line in Java
System.out.println("Please enter a floating point number from command line? ");
float decimal = inputReader.nextFloat();
System.out.println("You have entered : " + decimal);
//Java Example to get input from user using GUI
String input = JOptionPane.showInputDialog("Enter any number of your choice");
System.out.println("User has entered: " + input);
}
}
Output:
Please enter input from a command prompt:
Java Programming tutorial
Hi Java Programming tutorial
Please enter a number from the command line?
22
You have entered: 22
Please enter a floating-point number from the command line?
22.34
You have entered: 22.34
User has entered: 34343
That's all on how to get input from users in Java. I have already
discussed many other ways to get input from the command
line in my post 5 examples to get input from Console. Overall Java has
excellent support for getting input from the user which allows Java programmers at
beginner level to write simple programs driven from User input.
Other Java Beginners tutorials from Javarevisited Blog
And, now is the quiz time, what is difference between Console and Scanner class in Java? Which one is better to read input from command line and why?
3 comments :
What if the string has space in it?
Write a java program that gets an Array input at Run Time from the user and prints it in the console
Hello @Thambidurai, you may to check my post how to take array input from command line in Java. It solves the problem you mention above.
Post a Comment