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
- Read the age with
nextInt() - Call
nextLine()to consume the leftover newline - Read the message with
nextLine() - Print
Age: <age> - 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