Friday, 22 April 2016

How to get the standard input from console ?


Using Scanner
Scanner scanner = new Scanner(System.in); String input;
while (scanner.hasNextLine()) {
  input = scanner.nextLine(); 
  System.out.println(input);}

scanner.close();

Scanner methods : next, nextByte, nextShort, nextInt, nextLong, nextFloat, nextDouble


Using InputStreamReader
BufferedReader br = new BufferedReader(new InputStreamReader (System.in) );

String input;
while((input=br.readLine()) != null) {
   System.out.println(input);
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.