The hasNext method in Scanner determine whether scanner has another token in its input.
/**
* @author lautturi.com
* Java example:
*/
import java.util.*;
public class Lautturi {
public static void main(String[] args) {
// Scanner scanner = new Scanner(System.in);
Scanner scanner = new Scanner("hi,learning java in lautturi");
// the tokens are delimited by whitespace by default.
System.out.println("--first token--:");
// Print the first token
System.out.println(scanner.next());
System.out.println("--second token--:");
// Print the second token
if (scanner.hasNext()) {
System.out.println(scanner.next());
}
System.out.println("--rest tokens--:");
// Print the rest tokens
while (scanner.hasNext()) {
System.out.println(scanner.next());
}
// Close the scanner
scanner.close();
}
}
output:
--first token--: hi,learning --second token--: java --rest tokens--: in lautturi