Saturday, October 13, 2012

Java Program to get input from User from Console or command line- Example Tutorial Code

How to get input from user in Java from command line or console is one of the common thing ,every one started learning Java looks for. It’s second most popular way of starting programming after HelloWorld in Java. There are so many ways to get input from User in Java including command line and Graphical user interface. For beginner 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.

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.

Code Example
How to get Input from User in Java program with ExampleHere 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
 * 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
 * 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);
       
        //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 command prompt:
Java Programming tutorial
Hi Java Programming tutorial
Please enter a number from command line?
22
You have entered : 22
Please enter a floating point number from command line?
22.34
You have entered : 22.34
User has entered: 34343


That's all on how to get input from user in Java. I have already discussed many other ways to get input from command line in my post 5 example to get input from Console. Overall Java has excellent support for getting input from user which allows Java programmers at beginner level to write simple program driven from User input.

Other Java Beginners tutorials from Javarevisited Blog

No comments :

Post a Comment