Get user input in Java

By: Marcus  

This Java program prompts the user to enter the name by using System.out.print() method to keep the cursor on the same line. Then using the System.in object, along with the InputstreamReader and BufferedReader classes the user input is read. The br.readline() method reads the name from the command line. After pressing the enter key, you will get the user input.

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);
}
}  



Archived Comments

1. input can be taken simly with Console class
Console console= System.console();
int = c

View Tutorial          By: Dinesh at 2012-06-29 11:18:23

2. i need more examples
View Tutorial          By: Sathish at 2011-09-06 09:05:14

3. Your Work is Good, But i want to ask if there is any way of simplify it such that we will create a M
View Tutorial          By: Good Muyis at 2011-01-08 09:47:14


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial