Home Java Java-get-example Java get User Input



Java get User Input
Posted on: November 1, 2008 at 12:00 AM
In this section, you will learn how to get the user input from the command prompt. We are providing you a java application that will communicate with the user at the command line and returns the user input.

Java get User Input

     

In this section, you will learn how to get the user input from the command prompt. We are providing you a java application that will communicate with the user at the command line and returns the user input.

We have prompt the user to enter the name by using System.out.print() method to keep the cursor on the same line. Then we have used the System.in object, along with the InputstreamReader and BufferedReader classes in order to read the user input. The br.readline() method reads the name from the command line. After pressing the enter key, you will get the user input.

Here is the code of GetUserInput.java

import java.io.*;

public class GetUserInput {
    public static void main (String[] args) {
       System.out.print("Enter your name and press Enter: ");
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       String name = null;
       try {
         name = br.readLine();
       } catch (IOException e) {
         System.out.println("Error!");
         System.exit(1);
       }
       System.out.println("Your name is " + name);
}
}

Output will be displayed as:

Download Source Code

Related Tags for Java get User Input:
javaccomapplicationinputiousergetcommandvireturnlinethisidapppromptwithtolearneareilitsectioncommaliputusemanfrominmniccanicjhowproppcatsurnatishalleaandarvaunicassthavatiaphaticaicaplprndonomonp


More Tutorials from this section

Ask Questions?    Discuss: Java get User Input   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.