Question in Scanner | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Question in Scanner

Can you please advise why I am not getting the name displayed when the age is >16? import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); int age = input.nextInt(); String name = input.nextLine(); if (age < 16) { System.out.println("Too Young"); } else { System.out.println("Welcome!"+name); } } }

21st Sep 2016, 10:48 AM
Senthil Arumugam
1 Answer
0
You should use next() instead of nextLine(). Using next() will return what comes before a space and nextLine() automatically moves the scanner down after returning the current line.
30th Nov 2016, 11:23 AM
Felipe Cruz
Felipe Cruz - avatar