Java ProgramsBasics & I/ORead Multiple Values

Read Multiple Values in Java

beginner·  Basics & I/O  ·  I/O

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.

Input
25, 5.9, John
Output
Name: John, Age: 25, Height: 5.9

Java Program

Java
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

Enter your age: 25 Enter your height: 5.9 Enter your name: John Name: John, Age: 25, Height: 5.9

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.

How It Works
  1. 1sc.nextInt() reads the age, but leaves the newline character after it sitting in the input buffer.
  2. 2An extra sc.nextLine() right after consumes just that leftover newline, without printing anything or fetching a new value.
  3. 3sc.nextDouble() reads the height the same way, leaving another leftover newline that gets consumed by a second extra sc.nextLine().
  4. 4With the buffer clear, the final sc.nextLine() reads the name as intended, instead of immediately returning an empty string.
  5. 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.
  6. 6All three values are then printed together on one summary line.
Typing 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

ScannernextInt()nextDouble()nextLine()input buffer

Approach 2: BufferedReader

Java
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

Enter your age: 25 Enter your height: 5.9 Enter your name: John Name: John, Age: 25, Height: 5.9

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.

How It Works
  1. 1br.readLine() reads the age line as a String, which Integer.parseInt() converts to an int.
  2. 2br.readLine() reads the height line the same way, converted with Double.parseDouble().
  3. 3br.readLine() reads the name line directly as a String, needing no conversion at all.
  4. 4Because every call reads a full line and stops, there's never a stray newline left behind for the next read to trip over.
The same three lines — 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.

Key Concepts

BufferedReaderreadLine()Integer.parseInt()Double.parseDouble()

Related Programs