Read Multiple Values in Java
Problem
Mixing nextInt()/nextDouble() with nextLine() on the same Scanner needs an extra nextLine() call in between, since the numeric methods leave the trailing newline in the input buffer for nextLine() to pick up next.
Read an integer, a decimal number, and a line of text from the console, then print all three.
Java Program
import java.util.Scanner;
public class ReadMultipleValues {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println(age); // echo back — this compiler doesn't echo typed input itself
sc.nextLine(); // consumes the leftover newline after nextInt()
System.out.print("Enter your height: ");
double height = sc.nextDouble();
System.out.println(height);
sc.nextLine(); // consumes the leftover newline after nextDouble()
System.out.print("Enter your name: ");
String name = sc.nextLine(); // now reads the actual name line
System.out.println(name);
System.out.println("Name: " + name + ", Age: " + age + ", Height: " + height);
}
}Output
Core Logic
Reading three different types with the same Scanner needs one extra nextLine() call after each numeric read, to clear the leftover newline before the next nextLine() call.
- 1
sc.nextInt()reads the age, but leaves the newline character after it sitting in the input buffer. - 2An extra
sc.nextLine()right after consumes just that leftover newline, without printing anything or fetching a new value. - 3
sc.nextDouble()reads the height the same way, leaving another leftover newline that gets consumed by a second extrasc.nextLine(). - 4With the buffer clear, the final
sc.nextLine()reads the name as intended, instead of immediately returning an empty string. - 5Each value is echoed right after it's read, since most online compilers don't echo typed input themselves the way a real terminal does.
- 6All three values are then printed together on one summary line.
25, then 5.9, then John on separate lines reads age = 25, height = 5.9, and name = "John" correctly.Key Point: Skipping the extra nextLine() calls is a classic Scanner bug — without them, the name would silently come back as an empty string instead of "John".
Key Concepts
Approach 2: BufferedReader
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class ReadMultipleValuesBufferedReader {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your age: ");
int age = Integer.parseInt(br.readLine());
System.out.println(age); // echo back — this compiler doesn't echo typed input itself
System.out.print("Enter your height: ");
double height = Double.parseDouble(br.readLine());
System.out.println(height);
System.out.print("Enter your name: ");
String name = br.readLine(); // already a full line, no leftover-newline issue
System.out.println(name);
System.out.println("Name: " + name + ", Age: " + age + ", Height: " + height);
}
}
Output
Core Logic
BufferedReader reads one full line per value regardless of type, so there's no leftover-newline buffer issue to work around at all.
- 1
br.readLine()reads the age line as aString, whichInteger.parseInt()converts to anint. - 2
br.readLine()reads the height line the same way, converted withDouble.parseDouble(). - 3
br.readLine()reads the name line directly as aString, needing no conversion at all. - 4Because every call reads a full line and stops, there's never a stray newline left behind for the next read to trip over.
25, 5.9, John — are read cleanly in sequence with no extra consuming calls needed.Key Point: This is the real advantage BufferedReader has over Scanner for mixed-type input: since every read is line-based, the nextInt()-then-nextLine() gotcha simply doesn't exist here.