This question already has an answer here:
If you look at the line of code where it says
System.out.println("Please enter the firstname of your favourite female author");
mFirstName = scanner.nextLine();
System.out.println("Please enter her second name");
mSurname = scanner.nextLine();
It completely skips the firstname part and goes straight to surname? Any ideas why this is happenimh?
import java.util.*;
'class university{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
Person2 mPerson, fPerson;
String fFirstName, fSurname, mFirstName, mSurname;
int fAge, mAge;
System.out.println("Please enter the firstname of your favourite female author");
fFirstName = scanner.nextLine();
System.out.println("Please enter her second name");
fSurname = scanner.nextLine();
System.out.println("Please enter her age");
fAge = scanner.nextInt();
System.out.println("Please enter the firstname of your favourite female author");
mFirstName = scanner.nextLine();
System.out.println("Please enter her second name");
mSurname = scanner.nextLine();
System.out.println("Please enter her age");
mAge = scanner.nextInt();
System.out.print(fPerson);
}
}
fAge = scanner.nextInt();
does not consume the line ending.add
scanner.nextLine()
after that to absorb the end-of-line character and it will work.Add a
nextLine()
call after you callnextInt()
. BecausenextInt()
doesn't finish the line. So the next call tonextLine()
will return an empty string.