如何修复井字棋游戏加载时的Java NumberFormatException错误?
井字棋Multiplayer类存档加载异常修复
错误信息
Exception in thread "main" java.lang.NumberFormatException: For input string: "Matt" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at Multiplayer.multiplayer(Multiplayer.java:41) at TicTacToe.MUltiplayer_Game(TicTacToe.java:107) at TicTacToe.main(TicTacToe.java:38)
问题根源
- 保存与加载字段顺序不匹配:保存时写入顺序为
p1→p2→currRound→turn,但加载时读取顺序为currRound→p1→p2→turn,导致把玩家名称(字符串)当成回合数(整数)解析,触发NumberFormatException。 - 棋盘加载索引错误:读取棋盘行数据时,错误使用行索引
i获取列数组row的元素,应使用列索引j。
修复后的完整代码
//Creates a new 3x3 Array grid. char[][] ticTacToeBoard = new char[3][3]; char turn = 'X'; String winner; //Runs Multiplayer. public void multiplayer() { Scanner input = new Scanner(System.in); String p1 = "", p2 = ""; int currRound = 0; String loadOption; //Default as not used. boolean fileUsed = false; // Ask the user if they want to load a saved game System.out.print("Do you want to load a saved game? (y/n): "); loadOption = input.next(); //If user wants to load file. if (loadOption.substring(0, 1).equalsIgnoreCase("Y")) { String loadFile = "tictactoe_save.txt"; try (Scanner reader = new Scanner(new File(loadFile))) { fileUsed = true; // 修正读取顺序,与保存顺序一致 p1 = reader.nextLine(); p2 = reader.nextLine(); currRound = Integer.parseInt(reader.nextLine()); turn = reader.nextLine().charAt(0); System.out.println(p1 + ", " + p2 + ", " + turn + ", " + currRound); //For testing // 加载棋盘数据 for (int i = 0; i < ticTacToeBoard.length; i++) { String[] row = reader.nextLine().split(" "); for (int j = 0; j < Math.min(ticTacToeBoard[0].length, row.length); j++) { // 修正索引错误:使用列索引j而非行索引i ticTacToeBoard[i][j] = row[j].charAt(0); } } displayBoard(ticTacToeBoard); reader.close(); } catch (IOException e) { System.out.println("Error: File not found."); } } if (!fileUsed) { //If user does not want to load a save file. System.out.println("*Multiplayer active*"); System.out.print("Enter your name, Player 1: "); p1 = input.next(); System.out.print("Enter your name, Player 2: "); p2 = input.next(); System.out.println("\nLets play! Your goal is to get three-in-a-row \nwith your pieces to win the game! " + "Ties can occure, so be smart! \nPlayer 1 plays X pieces; Player 2 plays O pieces. Have fun!"); // 初始化空棋盘 for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { ticTacToeBoard[i][j] = ' '; } } displayBoard(ticTacToeBoard); currRound = 0; turn = 'X'; } // 根据turn初始化当前玩家 String currPlayer = turn == 'X' ? p1 : p2; //Game runs for (int round = currRound; round < 9; round++) { //User selects and places a piece that is legal. selectArea_Human(ticTacToeBoard, turn, currPlayer); //Updates ticTacToeBoard. System.out.println("-----------------------------------"); displayBoard(ticTacToeBoard); //Checks if three-in-a-row has occured. if (isThree_In_A_Row(turn, ticTacToeBoard)) { break; } //Next players turn. turn = (turn == 'X') ? 'O' : 'X'; currPlayer = (turn == 'X') ? p1 : p2; // Save the game state to a file after every turn. String saveFile = "tictactoe_save.txt"; try (FileWriter writer = new FileWriter(saveFile)) { writer.write(p1 + "\n"); // Save p1 name. writer.write(p2 + "\n"); // Save p2 name. writer.write((round + 1) + "\n"); // 保存下一个回合数,确保加载后正确续玩 writer.write(turn + "\n"); // Save the current turn. for (int i = 0; i < ticTacToeBoard.length; i++) { for (int j = 0; j < ticTacToeBoard[0].length; j++) { writer.write(ticTacToeBoard[i][j] + " "); } writer.write("\n"); } System.out.println("Game successfully saved."); } catch (IOException e) { System.out.println("Error: File not found."); } } //Checks for tie game; Otherwise declares the winner. if (!isThree_In_A_Row(turn, ticTacToeBoard)) { System.out.println("The game is over."); System.out.println("It's a tie!"); } else { winner = currPlayer; System.out.println("The game is over."); System.out.println("The winner is " + winner + "!"); } input.close(); }
额外修复说明
- 补充
turn变量初始化,避免未定义错误 - 修正当前玩家初始化逻辑,根据加载的
turn值正确匹配玩家 - 保存回合数改为
round + 1,确保加载后从下一个回合继续游戏 - 调整棋盘显示时机,加载完成后再显示存档棋盘
- 关闭输入扫描器,避免资源泄漏
内容的提问来源于stack exchange,提问作者Caleb Murnan
相关产品推荐
相关产品推荐

