0

I wrote this program called soccer team roaster. It stores player's jerseys number and player rating. And then does what ever the user wants to do with it within the menu option. But for some reason the menu option keeps on printing twice. I've been informed that its because when I ask for the int input for PlayerRating[i] it takes the Enter button and an input when I first ask for the user's input for String "menuOption". Can someone help?

Here is the code

import java.util.Scanner;

public class SoccerTeamRoaster {

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

        final int JERSY_NUMS = 5;
        int[] JersyNumber = new int[JERSY_NUMS];
        int[] PlayerRating = new int[JERSY_NUMS];
        String[] jPut = new String[JERSY_NUMS];
        int i = 0;
        boolean quit = false;
        for (i = 0; i < JERSY_NUMS; i++) {
            System.out.println("Enter player " + (i + 1) + "'s jersey number: ");
            JersyNumber[i] = sc.nextInt();
            System.out.println("Enter player " + (i + 1) + "'s rating: ");

            PlayerRating[i] = sc.nextInt();
            System.out.println("");

        }

        System.out.println("");
        System.out.println("ROSTER");
        int N = 0;
        for (N = 0; N < JERSY_NUMS; N++) {
            System.out.println("Player " + (N + 1) + " -- Jersey number: " + JersyNumber[N] + ", Rating: " + PlayerRating[N]);
        }

        while (!quit) {
            String Stg = "MENU\n"
                    + "u - Update player rating\n"
                    + "a - Output players above a rating\n"
                    + "r - Replace player\n"
                    + "o - Output roster\n"
                    + "q - Quit\n";
            System.out.println("");
            System.out.println(Stg);
            System.out.println("Choose an option: ");
            String menuOption = "?";
            menuOption = sc.nextLine();

            boolean correctInput = false;
            if (menuOption.equals("u") || menuOption.equals("a") || menuOption.equals("r") || menuOption.equals("o") || menuOption.equals("q")) {
                correctInput = true;
                menuOption = menuOption.trim();
            } else {
                correctInput = false;
            }

            if (menuOption.equals("u")) {
                System.out.println("Enter jersey number: ");
                int jerseyNum = sc.nextInt();

                System.out.println("New rating for player: ");
                int newRate = sc.nextInt();
                int M = 0;
                for (M = 0; M < JERSY_NUMS; M++) {

                    if (JersyNumber[M] == jerseyNum) {
                        PlayerRating[M] = newRate;
                    }
                }
            } else if (menuOption.equals("a")) {
                System.out.println("Enter a rating: ");
                int rating = sc.nextInt();
                int k = 0;
                for (k = 0; k < JERSY_NUMS; k++) {
                    if (PlayerRating[k] > rating) {
                        System.out.println("Player " + (k + 1) + " -- Jersey Number: " + JersyNumber[k] + ", Rating: " + PlayerRating[k]);
                    }
                }

            } else if (menuOption.equals("o")) {
                System.out.println("ROSTER");
                int J = 0;
                for (J = 0; J < JERSY_NUMS; J++) {
                    System.out.println("Player " + (J + 1) + " -- Jersey number: " + JersyNumber[J] + ", Rating: " + PlayerRating[J]);
                }
            } else if (menuOption.equals("q")) {
                quit = true;
            } else if (menuOption.equals("r")) {
                System.out.println("Enter jersey number: ");
                int jerNum = sc.nextInt();
                int l = 0;
                for (l = 0; l < JERSY_NUMS; l++) {
                    if (JersyNumber[l] == jerNum) {
                        System.out.println("Enter new jersey number: ");
                        JersyNumber[l] = sc.nextInt();
                        System.out.println("Enter new player rating: ");
                        PlayerRating[l] = sc.nextInt();
                    }
                }
                int a = 0;
                for (a = 0; a < JERSY_NUMS; a++) {

                    System.out.println("Player " + (a + 1) + " -- Jersey number: " + JersyNumber[a] + ", Rating: " + PlayerRating[a]);
                }

            }

        }

        return;
    }

}
Elrond_EGLDer
  • 47,430
  • 25
  • 189
  • 180

2 Answers2

1

The reason why it prints two times is because of this code...

menuOption = sc.nextLine();

just change it to

menuOption = sc.next();

now, it only prints one time

0

You use Scanner#nextInt to get jersey numbers and rates. But Scanner#nextInt does not consume the last newline character of input, and thus that newline character is consumed when Scanner#nextLine is called after that.

So you have to call Scanner#nextLine once to throw aray the newline character before while loop.

nakano531
  • 468
  • 7
  • 13