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?