-2

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);
    }
}
Ceri Westcott
  • 59
  • 1
  • 8
  • This is likely caused by the `scanner.nextInt`, which will leave a new line character in the stream, which is then being picked by the next `scanner.nextLine`. Try adding a `scanner.nextLine` call BEFORE the next question – MadProgrammer Oct 27 '13 at 22:39

2 Answers2

2

Add a nextLine() call after you call nextInt(). Because nextInt() doesn't finish the line. So the next call to nextLine() will return an empty string.

Martijn Courteaux
  • 63,780
  • 43
  • 187
  • 279
1

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.

Jeroen Vannevel
  • 41,258
  • 21
  • 92
  • 157