Saturday 13 February 2016

Accept User Input in Java

For accepting user input in Java refer the following piece of code below :

import java.io.*;

public class User_Input {

public static void main(String[] args) {
String inp="";
System.out.println("Please enter your name : ");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
inp=br.readLine();
}
catch(IOException e) {
e.printStackTrace();
}
System.out.println("Welcome " +inp);

}


}

Output:
Please enter your name : 
IronCladWriter
Welcome IronCladWriter

Make sure to import java.io.* so as to use the input/output functions. Notice that we have wrapped an InputStreamReader within a BufferedReader. Guys, do check out detailed documentation on BufferedReader and InputStreamReader for better understanding.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker