002-002-011

Solving the nextLine and nextInt Newline Issue

Hard

Problem Description

Solving the nextLine and nextInt Newline Issue

In this problem, you will create a program that reads an integer age with nextInt(), consumes the leftover newline character, then reads a message string with nextLine(), and displays both results to standard output.

Learning Objective: Understand and resolve the newline consumption issue when calling nextLine() after nextInt()

Overview

Create a program that reads an age (integer) followed by a message (string), correctly handling the newline consumption issue.

Specifications

  1. Read the age with nextInt()
  2. Call nextLine() to consume the leftover newline
  3. Read the message with nextLine()
  4. Print Age: <age>
  5. Print Message: <message>

Input Example

25
Hello World

Output Format

Age: 25
Message: Hello World

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up