Taking inputs from users
5 April 2007There are a number of ways to take input from user in Java.
Following code sample helps you in taking input as a character, as a stream and from GUI.
Required Packages: import java.io and javax.swing packages.
// reading character from user char a = (char)System.in.read(); System.out.println("Character read is: " + a); // taking input from GUI String input = JOptionPane.showInputDialog("What is your name?"); System.out.println("Your name is: " + input); // taking input in stream // creating an InputStreamReader using the standard input stream. InputStreamReader i_stream = new InputStreamReader( System.in ); // creating BufferedReader using the InputStreamReader. BufferedReader b_reader = new BufferedReader( i_stream ); // prompting the user System.out.print( "What is your name? " ); // usingBufferedReader to read a line String input = b_reader.readLine(); // displaying input with welcome message System.out.println( "Welcome " + input ); // One easy way is to create a single shared BufferedReader // for keyboard input and reuse it whereever needed. private static BufferedReader b_reader = new BufferedReader( new InputStreamReader( System.in ) ); public static void main(String[] args) throws Exception { // prompting the user for input System.out.print( "What is your name? " ); // taking name input String input_name = b_reader.readLine(); // prompting the user for input System.out.print( "What is your age? " ); // taking age input which is integer // we have to convert String to Integer int input_age = Integer.parseInt(b_reader.readLine()); // displaying name input with welcome message System.out.println( "Welcome " + input_name ); // displaying age input System.out.println( "You are " + input_age + " years old"); System.out.println( "Half of which is " + (input_age/2)); // reading character from user char a = (char)System.in.read(); System.out.println("Character read is: " + a); // taking input from GUI String input = JOptionPane.showInputDialog("What is your name?"); System.out.println("Your name is: " + input); // taking input in stream // creating an InputStreamReader using the standard input stream. InputStreamReader i_stream = new InputStreamReader( System.in ); // creating BufferedReader using the InputStreamReader. BufferedReader b_reader = new BufferedReader( i_stream ); // prompting the user System.out.print( "What is your name? " ); // usingBufferedReader to read a line String input = b_reader.readLine(); // displaying input with welcome message System.out.println( "Welcome " + input ); // One easy way is to create a single shared BufferedReader // for keyboard input and reuse it whereever needed. private static BufferedReader b_reader = new BufferedReader( new InputStreamReader( System.in ) ); public static void main(String[] args) throws Exception { // prompting the user for input System.out.print( "What is your name? " ); // taking name input String input_name = b_reader.readLine(); // prompting the user for input System.out.print( "What is your age? " ); // taking age input which is integer // we have to convert String to Integer int input_age = Integer.parseInt(b_reader.readLine()); // displaying name input with welcome message System.out.println( "Welcome " + input_name ); // displaying age input System.out.println( "You are " + input_age + " years old"); System.out.println( "Half of which is " + (input_age/2));
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: Taking inputs from users
And with Java 6 we can use Console too.
Console console = System.console();
String username = console.readLine( “Name” );
console.printf( “Willkommen %s.”, username );