Input loop is part of Short Circuit's Console Program Basics selection.
Task

Read from a text stream either word-by-word or line-by-line until the stream runs out of data.

The stream will have an unknown amount of data on it.




import java.io.InputStream;
import java.util.Scanner;
 
public class InputLoop {
    public static void main(String args[]) {
        // To read from stdin:
        InputStream source = System.in;
 
        /*
        Or, to read from a file:
        InputStream source = new FileInputStream(filename);
 
        Or, to read from a network stream:
        InputStream source = socket.getInputStream();
        */
 
        Scanner in = new Scanner(source);
        while(in.hasNext()){
            String input = in.next(); // Use in.nextLine() for line-by-line reading
 
            // Process the input here. For example, you could print it out:
            System.out.println(input);
        }
    }
}