Java 加载/保存GUI游戏--NoTouchElementException错误

Java 加载/保存GUI游戏--NoTouchElementException错误,java,user-interface,Java,User Interface,我对在Java中使用GUI和JPanel非常陌生。现在我正在开发一款可以保存和加载的游戏。保存时,它会使用JArray,并根据某个位置或某张图片上是否没有任何内容,将0s、1s和2s保存到文件中。加载时,它会查看0s、1s和2s,并在相应的位置(在for循环中)用图片或其他内容替换它们 当我加载我的游戏时,它几乎不会加载,并且会以“NoTouchElementException”出错。这是我的加载: public void loadGame() throws FileNotFoundExcep

我对在Java中使用GUI和JPanel非常陌生。现在我正在开发一款可以保存和加载的游戏。保存时,它会使用JArray,并根据某个位置或某张图片上是否没有任何内容,将
0s、1s和2s
保存到文件中。加载时,它会查看
0s、1s和2s
,并在相应的位置(在for循环中)用图片或其他内容替换它们

当我加载我的游戏时,它几乎不会加载,并且会以“
NoTouchElementException
”出错。这是我的加载:

 public void loadGame() throws FileNotFoundException{

        String fileName = JOptionPane.showInputDialog("What is your file's name?") + ".txt";
        Scanner reader = new Scanner(new FileReader(fileName));
         playerOne = reader.next();
         playerTwo = reader.next();
         counter = reader.nextInt();
        for (int i = 1; i < grid_height-1; i++) {
            for (int j = 1; j <= grid_width-1; j++) {

                    if(reader.nextInt() == 1){game[i][j].setIcon(p1);}
                    if(reader.nextInt() == 2){game[i][j].setIcon(p2);} 
                    else if(reader.nextInt() == 0){game[i][j].setIcon(null); } } } 

    reader.close(); 
} 

    }
public void loadGame()抛出FileNotFoundException{
String fileName=JOptionPane.showInputDialog(“文件名是什么?”)+“.txt”;
扫描仪阅读器=新扫描仪(新文件阅读器(文件名));
playerOne=reader.next();
playerTwo=reader.next();
计数器=reader.nextInt();
对于(int i=1;i对于(int j=1;j您应该正确保存的返回值,并将其用于比较

public void loadGame() throws FileNotFoundException{

       //your code here ........
        for (int i = 1; i < grid_height-1; i++) {
            for (int j = 1; j <= grid_width-1; j++) {
                    if(reader.hasNext()){
                         int next = reader.nextInt(); //save the return value and use it for comparision

                         if (next == 1){game[i][j].setIcon(p1);}
                         if (next  == 2){game[i][j].setIcon(p2);} 
                         else if(next  == 0){game[i][j].setIcon(null);
                    } //close the if loop
        //rest of your code here .......
public void loadGame()抛出FileNotFoundException{
//您的代码在这里。。。。。。。。
对于(int i=1;i对于(int j=1;j),i和j循环有些可疑(

public void loadGame() throws FileNotFoundException{

       //your code here ........
        for (int i = 1; i < grid_height-1; i++) {
            for (int j = 1; j <= grid_width-1; j++) {
                    if(reader.hasNext()){
                         int next = reader.nextInt(); //save the return value and use it for comparision

                         if (next == 1){game[i][j].setIcon(p1);}
                         if (next  == 2){game[i][j].setIcon(p2);} 
                         else if(next  == 0){game[i][j].setIcon(null);
                    } //close the if loop
        //rest of your code here .......