1

i am a new member. Currently i'm having a problem with this Player class that belong to my little project. Whenever I enter a string name, it requires me to Enter again to finish:

 public void setName(StringBuffer str) {
        for (int i = 0; i < str.length(); i++) {
            Character c = str.charAt(i);
            if (i == 0)
                str.replace(i, i + 1, Character.toUpperCase(c) + "");
            if (c == ' ') {
                Character x = str.charAt(i + 1);
                str.replace(i + 1, i + 2, Character.toUpperCase(x) + "");
            }
            if ((Character.isLetter(c)) && (i < str.length() - 1)) {
                Character x = str.charAt(i + 1);
                if (x != ' ') {
                    str.replace(i + 1, i + 2, Character.toLowerCase(x) + "" );
                }
            }
        }
        name = str.toString();
    }

And here is the main class:

import java.util.Scanner;
public class main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int NUM_PLAYER = 4;
        int choice;
        int playerChoice;
        String name;

        Matrix matrix = new Matrix();
        Player[] player = new Player[NUM_PLAYER];
        do {
            System.out.println("THE LAST PASSENGER\n");
            System.out.println("1. Create Matrix");
            System.out.println("2. Change color of the Matrix");
            System.out.println("3. Input information of players and play the game");
            System.out.println("4. Play the game");
            System.out.println("5. Display");
            System.out.println("6. Highest and Lowest score");
            System.out.println("7. Exit");
            System.out.print("Enter your choice: ");
            choice = sc.nextInt();
            switch (choice) {
                case 1: System.out.println(matrix);
                break;

                case 2: matrix.swap();
                break;

                case 3:
                for (int i = 0; i < NUM_PLAYER; i++) {
                    player[i] = new Player();
                    sc.nextLine();
                    System.out.print("Input the name of player " + (i + 1) + ": ");
                    name = sc.nextLine();
                    StringBuffer str = new StringBuffer(name);
                    player[i].setName(str);
                }
                break;

                case 4: for (int i = 0; i < NUM_PLAYER; i++) {
                    player[i] = new Player();
                    System.out.print("Input the color that player " + (i + 1));
                    System.out.print(" has chosen(BLUE = 0, YELLOW = 1, RED = 2): ");
                    playerChoice = sc.nextInt();
                    while ((playerChoice < 0) || (playerChoice > 2)) {
                        System.out.print("Input valid. Please choose again(BLUE = 0, YELLOW = 1, RED = 2): ");
                        playerChoice = sc.nextInt();
                    }
                    player[i].setColor(playerChoice);
                    for (int j = 0; j < 5; j++) {
                        System.out.print("Input position in the row " + (j + 1) + " wants to choose: ");
                        playerChoice = sc.nextInt() - 1;
                        while ((playerChoice < 0) || (playerChoice > 2)) {
                            System.out.print("Invalid input. Please input from 1 to 3: ");
                            playerChoice = sc.nextInt() - 1;
                        }
                        int check = matrix.getColorMatrix(j, playerChoice);
                        if (player[i].getColor() == check) {
                            System.out.println("Correct!!!");
                            player[i].scoreIncrease();
                        }
                        else {
                            System.out.println("Wrong choose.");
                            break;
                        } System.out.println("Your score is: " + player[i].getScore());
                    }
                }
                break;

                case 5:
                    System.out.printf("%-20s%-10s%-10s\n", "Name", "Color", "Score");
                    for (Player i : player) {
                        System.out.printf("%-20s%-12s%-10d\n", i.getName(), i, i.getScore());
                    }
                    break;
                case 6:
                    int highest = 0;
                    int lowest = 0;
                    for (int i = 0; i < NUM_PLAYER; i ++){
                        int score = player[i].getScore();
                        if (score > highest) {
                            highest = score;
                        }
                        if (score < lowest) {
                            lowest = score;
                        }
                    }

                    System.out.print("Player have the highest score is: ");
                    for (int i = 0; i < NUM_PLAYER; i++)
                        if (highest == player[i].getScore())
                            System.out.println(player[i].getName());

                    System.out.print("Player have the lowest score is: ");
                    for (int i = 0; i < NUM_PLAYER; i++)
                        if (lowest == player[i].getScore())
                            System.out.println(player[i].getName());
                    break;
                default:
                    System.out.println("There is no choice " + choice);
                    System.out.println("Please Enter from 1 to 7.");
                    break;
            }
            System.out.println();
        } while (choice != 7);
    }
}

How can I fix this problem? Update: it's Scanner problem and i should replace it with BufferedReader but i not familiar with it so what should i do?

SupaChat99
  • 21
  • 2
  • 2
    Please post a minimal reproducible example https://stackoverflow.com/help/minimal-reproducible-example so we can help you better. – Asis May 22 '21 at 16:05
  • 1
    As a new user here you probably could not know: It’s a *very* good idea to keep an eye on your question the first few hours after you have posted it to see any requests for clarification, depth, other information, etc. Replying promptly to such is your best chance that other users are both willing and able to help you. Interest in the question can fade rather rapidly after the first hour or so. – Ole V.V. May 22 '21 at 16:33

1 Answers1

0

Your problem is here:

                player[i] = new Player();
                sc.nextLine(); // <-- Why??!
                System.out.print("Input the name of player " + (i + 1) + ": ");
                name = sc.nextLine();
                StringBuffer str = new StringBuffer(name);
                player[i].setName(str);

The call to sc.nextLine() in the second code line I quoted causes your program to wait until the user presses Enter.

In a loop, the first time through nextLine() simply consumes the Enter that the user pressed after entering the choice (3). Every subsequent time through the loop the user again needs to press Enter here. So you may say that if you had only 1 player, there would not have been any problem.

A quick fix is to move that line up to right after the line where the choice is entered instead:

        choice = sc.nextInt();
        sc.nextLine(); // Consume the Enter that the user pressed after the choice

Link: A related question that will give you more information about how Scanner works: Scanner is skipping nextLine() after using next() or nextFoo()?

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117