How to Read from stdin in Java

Reading from stdin in java looks like in most languages. Standard in is bound to System.in - and must be opened as if it where a file just like in c.

Then you can iterate over the lines like in Ruby - and print it to stdout with System.out.print

import java.util.*;
class Stdin
{
  public static void main (String [] args) throws java.io.IOException
  {
    Scanner input = new Scanner(System.in);
    while(input.hasNextLine())
    {
      System.out.print (input.nextLine()+"\n");
    }
  }
}

Related Posts