View RSS Feed

My Java Tips

Taking inputs from users

Rate this Entry
by , 11-08-2011 at 07:28 PM (1313 Views)
There 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.



Java Code:
// 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));

Submit "Taking inputs from users" to Facebook Submit "Taking inputs from users" to Digg Submit "Taking inputs from users" to del.icio.us Submit "Taking inputs from users" to StumbleUpon Submit "Taking inputs from users" to Google

Comments