Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
System.in is Java's standard input stream, but you do not call nextInt() or nextLine() directly on it. A Scanner reads from that stream and provides methods for different kinds of input. You import Scanner, create one connected to System.in, and then call the method that matches the value you need.
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
int age = scanner.nextInt();For the running input, the first line contains 19, so scanner.nextInt() returns 19 and stores that result in age. The import makes the Scanner class available, the constructor connects it to System.in, and nextInt() performs the read.
Given the input whose first line is 19, what does scanner.nextInt() return?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The first input line is made of the characters 1, 9, and a line separator. When nextInt() reads the integer, it consumes 1 and 9 because those characters form the result 19. It does not consume the line separator that follows the integer. The Scanner position is therefore between 9 and the separator, not at the beginning of the next line.
nextLine() uses a different boundary. Starting at the current Scanner position, it reads every remaining character through the end of the current line, then consumes that line's separator. It does not search for the next nonempty line. After nextInt(), the current line has no characters left before its separator, so an immediate nextLine() reads an empty string.
This sequence looks as if it should put Mira Sen into name, but it does not. The first nextLine() starts immediately before the separator left after 19. It finishes the first line and returns an empty String. The second input line is still waiting to be read.
int age = scanner.nextInt();
String name = scanner.nextLine();
System.out.println("Age: " + age);
System.out.println("Name: " + name);Add one nextLine() after nextInt(). That first call consumes the leftover separator and returns an empty string that you ignore. The following nextLine() then starts at the first character of the second line and captures the complete name, including the space between Mira and Sen.
int age = scanner.nextInt();
scanner.nextLine();
String name = scanner.nextLine();Insert the missing statement so that name becomes the full second line, Mira Sen.
int age = scanner.nextInt();
[missing statement]
String name = scanner.nextLine();Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Use nextInt() when the next value is an integer token, and use nextLine() when the value extends to the end of a line. The first input field must be a valid integer for nextInt() to read it. For the second line, nextLine() is necessary because the name contains a space. next() would stop at that space and capture only Mira.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int age = scanner.nextInt();
scanner.nextLine();
String name = scanner.nextLine();
System.out.println("Age: " + age);
System.out.println("Name: " + name);
}
}With input 19 on the first line and Mira Sen on the second, nextInt() gives age the value 19. The inserted nextLine() consumes the first line separator, and the next nextLine() gives name the value Mira Sen. The two print statements therefore produce the labeled lines Age: 19 and Name: Mira Sen.
Age: 19
Name: Mira Sen