0

I am doing a simple Java programme. But when I run it, the two input for the first name and last name shows at the same time. What can the problem be?

Here's the code and the screencap of what I am doing: input for the first and last name shows at the same time

import java.util.Scanner;

public class GenderGame {

    static String fName, lName;
    static byte age;

    public static void main(String[] args) {
        char selectGender;
        Scanner scanner = new Scanner(System.in);

        System.out.println("What is your gender? (M or F): ");
        selectGender = scanner.next().charAt(0);
        if (selectGender == 'M') {
            genderMale(scanner);
        } else if (selectGender == 'F') {
            genderFemale(scanner);
        }
    }

    private static void genderMale(Scanner scanner) {
        System.out.println("First name: ");
        fName = scanner.nextLine();
        System.out.println("Last name: ");
        lName = scanner.nextLine();
        System.out.println("Age: ");
        age = scanner.nextByte();

        System.out.println("Full name: Mr. " + fName + " " + lName + "\n" +
                "Age: " + age);
    }

    private static void genderFemale(Scanner scanner) {
        System.out.println("First name: ");
        fName = scanner.nextLine();
        System.out.println("Last name: ");
        lName = scanner.nextLine();
        System.out.println("Age: ");
        age = scanner.nextByte();

        System.out.println("Full name: Ms. " + fName + " " + lName + "\n" +
                "Age: " + age);
    }
}
clieg
  • 101
  • 1

0 Answers0