Created
January 12, 2016 08:51
-
-
Save indzi/0ca2ca3cbaf9fa6cc482 to your computer and use it in GitHub Desktop.
Taking user input in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//You can use any of the following options based on the requirements. | |
//Scanner class | |
Scanner scan = new Scanner(System.in); | |
String s = scan.next(); | |
int i = scan.nextInt(); | |
//BufferedReader and InputStreamReader classes | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String s = br.readLine(); | |
int i = Integer.parseInt(br.readLine()); | |
//DataInputStream class | |
DataInputStream dis = new DataInputStream(System.in); | |
int i = dis.readInt(); | |
The readLine method from the DataInputStream class has been deprecated. To get String value, you should use the previous solution with BufferedReader | |
//Console class | |
Console console = System.console(); | |
String s = console.readLine(); | |
int i = Integer.parseInt(console.readLine()); | |
//Apparently, this method does not work well in some IDEs. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment