Reading until the input ends
This module is about reading data from files. A program reads a file one line at a time, much as it reads what a user types, so it starts at the keyboard.
Module 19 read input in a while (true) loop that stops at a signal, such as an empty line: read a line, break if it's the signal, and only then use it. This step adds two more ways to know when to stop.
Until the input ends
A Scanner's hasNextLine() tells whether there's another line to read. It's true while there is one, and false once the input has ended, so a loop can read every line without waiting for a signal:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int count = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
count++;
System.out.println(count + ": " + line);
}
System.out.println("Lines: " + count);
}
}
red
green
blue1: red
2: green
3: blue
Lines: 3hasNextLine()only looks.nextLine()then reads the line, so the next round sees the line after it.- An empty line is a line too:
hasNextLine()istruefor it, andnextLine()gives"". - In the checks on this site, the input ends after its last line. A person at a keyboard has no natural end, which is why keyboard programs usually wait for a stop signal instead. (In a terminal, Ctrl+D ends the input, or Ctrl+Z and then Enter on Windows.)
A file does have an end, and this loop is exactly how the next steps read one.
A stop word in any capitals
A stop word typed by a person may come as end, End or END. equalsIgnoreCase from module 16 accepts all of them:
String line = scanner.nextLine();
if (line.equalsIgnoreCase("end")) {
break;
}