+ 1

i have logical error how i get input after integer input in java

import java.util.Scanner; public class gradingSystem { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("What is Your Your: "); String name = in.nextLine(); System.out.print("How many people working:"); int no_people = in.nextInt(); System.out.print("What is Your status name: "); String status = in.nextLine(); } }

3rd Mar 2021, 3:41 AM
Muhammad Hamza
Muhammad Hamza - avatar
2 Answers
+ 1
Read a line after reading int for <no_people>. Scanner.nextInt() leaves a '\n' in the input stream buffer which failed the Scanner.nextLine() call for reading <status>. Calling in.nextLine() after the call to Scanner.nextInt() consumes the leftover '\n', allowing successive Scanner.nextLine() call to read <status> correctly. int no_people = in.nextInt(); in.nextLine (); // read <status> here
3rd Mar 2021, 5:09 AM
Ipang
+ 1
#Ipang thanks you very much
3rd Mar 2021, 5:43 AM
Muhammad Hamza
Muhammad Hamza - avatar