Home Java Java-get-example Java get User Input
Questions:Ask|Latest


 
 

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

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

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

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Richa Singh
April 2, 2011
Give input from user

import java.util.*; class A { public static void main(String []args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the age"); int age=sc.nextInt(); System.ot.println(age); } }
deepro
April 4, 2011
input in java

thanks.....it worked cool
guru
April 12, 2011
web site

i create a new web page .. in the web page i need only content details..
Gaurav
May 28, 2011
Java

Rose India is playing a good role in incorporating new technological skills to the youth of the world.
raj
June 29, 2011
please clarify this doubt.

I want a JAVA program by using some conditions,such as, 1) java program using separate classes(one or two classes(not inheritance)) 2)the input of the program must given by user (i.e. input given by user at run time.(don't give the required input with in a program)) 3. without exception handling technique(i.e. don't use try, catch and throw block) Is it possible or not? possible means please give some example for this.
sartak
January 16, 2013
java

great nice solution
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