I'm trying to ask someone for a string (can't be an integer because the rows are numbered A-C). However, the re-assignment code between the if statements is not working.
Input:
System.out.println("Player " + player + " please enter the row. (A-C)");
String userRow = sc.nextLine();
System.out.println("Player " + player + " please enter the column. (1-3)");
String userCol = sc.nextLine();
System.out.println("userRow is " + userRow);
if (userRow == "A" || userRow == "a" || userRow == "1")
userRow = "0";
if (userRow == "B" || userRow == "b" || userRow == "2")
userRow = "1";
if (userRow == "C" || userRow == "c" || userRow == "3")
userRow = "2";
System.out.println("After transformation: userRow is " + userRow);
int exactRow = Integer.parseInt(userRow);
Output:
Player X please enter the row. (A-C)
A
Player X please enter the column. (1-3)
3
userRow is A
After transformation: userRow is A
Exception in thread "main" java.lang.NumberFormatException: For input string: "A"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
Aucun commentaire:
Enregistrer un commentaire