Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Java-get-example Java get User Input

Related Tutorials


 
 

Share on Google+Share on Google+

Java get User Input

Advertisement
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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 1, 2008

Related Tutorials

Discuss: Java get User Input   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:15
Prateek
January 22, 2012
Program

Sir My Program is a text file has name a.txt that have some data like name price sugar 10 sugar 12 tea 10 tea 15 i want to read , when i type sugar then all related sugar time are show in command window.
Devdyuti singh
January 24, 2012
BufferedReader

program to calculate sum and average of 10 numbers ,where numbers are taken from user ....
hari prasad
April 17, 2012
Question

how many ways we read inputs using command promt in java? please send answer for me.....
Norlin Hundirwa
May 29, 2012
input code in java

i dont know how it has worked for others but i am getting the errors java.lang.NoSuchMethodError:main Exception in thread "main"
chitransh jain
June 28, 2012
java

this is good to understand...
Durga
August 2, 2012
Get User input in Java

Can I get a code which takes a integer input from the user, manipulates the same value the displays a new value
Govind
August 13, 2012
Nice

Nice
aqsa
December 23, 2012
problem

it is not running plx help i copied whole thing ,i hav no idea ?
D
April 17, 2014
Thanks

Just what I needed! :-)
DMCA.com