我在運行以下代碼並輸入5x5的大小時出現此錯誤。不知道爲什麼?NumberFormatException的Java錯誤
當我輸入10x10它似乎運行良好,但我不知道輸出是否正確。
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)"
這裏是我的代碼
import java.util.Scanner;
public class CheckerBoard {
public static void main(String [] args){
Scanner userInput = new Scanner(System.in);
System.out.println("What two colors would you like your board to be?");
String colorOne = userInput.next();
String colorTwo = userInput.next();
do {
System.out.println("How big should the checker board be? (Square sizes only please)" + "\n"
+ "Please enter it like 4x4 with whatever numbers you choose.");
String boardSize = userInput.next();
int intOne = Integer.parseInt(boardSize.substring(0,boardSize.indexOf("x")));
System.out.println(boardSize.indexOf("x"));
int intTwo = Integer.parseInt(boardSize.substring(boardSize.indexOf("x")+1, boardSize.length()-1));
System.out.println(intOne);
} while(false);
}
}
//keep in mind that this program is not done yet, this is just a current issue I am having atm.
如果您可以指定異常來自的行,這將有所幫助。對我來說,你似乎至少在一個子串方法調用中使用了錯誤的索引。 –