Java 无数的格式概念到处都是。为什么?(parseInt)

Java 无数的格式概念到处都是。为什么?(parseInt),java,exception,parseint,numberformatexception,text-based,Java,Exception,Parseint,Numberformatexception,Text Based,第一次在这里贴海报,有一个非常烦人的问题。我决定用Java创建一个基于文本的RPG,以巩固我所学的内容。对于所有冗长的游戏,保存/加载系统是必要的。这就是我的问题所在 我已经创建了一个菜单系统,效果很好。然而,当我决定开始一个新的游戏时,我会从左、右和中得到一些形式概念。我的源代码在下面,任何帮助都将非常感谢。我已将每个重要部分分离为单独的Java文件 //Imports import java.io.IOException; import java.io.File; import java.i

第一次在这里贴海报,有一个非常烦人的问题。我决定用Java创建一个基于文本的RPG,以巩固我所学的内容。对于所有冗长的游戏,保存/加载系统是必要的。这就是我的问题所在

我已经创建了一个菜单系统,效果很好。然而,当我决定开始一个新的游戏时,我会从左、右和中得到一些形式概念。我的源代码在下面,任何帮助都将非常感谢。我已将每个重要部分分离为单独的Java文件

//Imports
import java.io.IOException;
import java.io.File;
import java.io.*;
import java.util.Scanner;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

//'Choices' - By Yemi Oladimeji/YemZeee

class Choices{
        public static void main(String [] args) throws IOException{

    Scanner input = new Scanner(System.in);
    String startChoice = null;

    //Introduction begin
        GameMethods.space();
        System.out.println("########  ##    ## ######## ######## ########            ######## ########");
        System.out.println("##        ##    ## ##    ##    ##    ##       ##       ##        ");
        System.out.println("##        ##    ## ##    ##    ##    ##       ##       ##        ");
        System.out.println("##        ######## ##    ##    ##    ##       ######    ###### ");
        System.out.println("##        ##    ## ##    ##    ##    ##       ##             ##");
        System.out.println("##        ##    ## ##    ##    ##    ##       ##             ##");
        System.out.println("########  ##    ## ######## ######## ######## ######## ########");
        GameMethods.space();
        System.out.println("Greetings, and welcome to Choices, a 2D text-based RPG!");
        System.out.println("What would you like to do?");
        System.out.println("<P>lay :: <A>bout :: <Q>uit");
        startChoice = input.nextLine();
        GameMethods.sChoice(startChoice);
        }
}//End Choices.java
引发的异常是:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:504)
    at java.lang.Integer.parseInt(Integer.java:527)
    at GameMethods.sChoice(GameMethods.java:59)
    at Choices.main(Choices.java:32)    
我意识到这是一个很大的要求,特别是第一次海报,但我在这里的想法真的用完了。请随意复制并运行代码。如果有任何答案,我们将不胜感激。再次感谢。

同意;堆栈跟踪正在告诉您出了什么问题。查看您试图从保存文件中读取四行四个整数的代码,但是*Area()方法中的保存逻辑只写入一个整数。也许你打算写一个整数和三个零


最好使用一个整数来指示保存文件中的位置(0=森林、1=城市、2=岩浆等),而不是在单独的行上使用四个0/1整数。

我只读取了一行(堆栈跟踪的第一行),但它告诉您不能将空字符串解析为数字。很明显,您的文件包含空行,或者查找和/或读取文件时出现问题。您应该使用基本的调试步骤(如使用调试器)对此进行故障排除。您正在读取的文件似乎是空的…它有初始行,或者其余4行中的任何一行是空的。以下是一些我花了太长时间才遵循的好建议:以小步编写程序。如果程序的一部分必须从文件中读取,那么创建一个可以读取文件的非常简单的测试程序。当你开始工作时,把它融入到你的项目中,并进入下一个小步骤。
import java.io.*;
import java.util.Scanner;
import java.io.IOException;

public class ChoiceSaveMethods{

//Area variables:
static int[] areaMarkers = new int[4];
static int[] areaChoices = new int[4];
static Scanner input = new Scanner(System.in);


public static void forestArea(){
    GameMethods.space();
    System.out.println("Area 1: Forest. What would you like to do here?: ");
    System.out.println("1 - Save the game?");
    System.out.println("2 - View a description of the area?");
    System.out.println("3 - Progress to the next area?");
    System.out.println("4 - Exit to the main menu?");

   areaChoices[0] = input.nextInt();

   if(areaChoices[0] == 1){
        try{
            FileWriter saveFile1 = new FileWriter("C:\\Users\\Yemi\\Desktop\\TheGame\\SaveFiles\\TextSave1.txt");
            areaMarkers[0] = 1;
            saveFile1.write(1);
            System.out.println("Game has been saved successfully.");
            saveFile1.close();
            forestArea();
        } catch (IOException ex){
            ex.printStackTrace();
        } 
    } else if(areaChoices[0] == 2){
        System.out.println("This is a forest. It has trees.");
        forestArea();
    } else if(areaChoices[0] == 3){
        System.out.println("Progressing to next area...");
        cityArea();
    } else if(areaChoices[0] == 4){
        System.out.println("Returning to main menu.");
        GameMethods.menuCall();
    }
}

public static void cityArea(){
    GameMethods.space();
    System.out.println("Area 2: City. What would you like to do here?: ");
    System.out.println("1 - Save the game?");
    System.out.println("2 - View a description of the area?");
    System.out.println("3 - Progress to the next area?");
    System.out.println("4 - Exit to the main menu?");

    areaChoices[1] = input.nextInt();

    if(areaChoices[1] == 1){
        try{
            FileWriter saveFile2 = new FileWriter("C:\\Users\\Yemi\\Desktop\\TheGame\\SaveFiles\\TextSave1.txt");
            areaMarkers[1] = 1;
            saveFile2.write(areaMarkers[1]);
            System.out.println("Game has been saved successfully.");
            saveFile2.close();
            cityArea();
        } catch(IOException ex){
            ex.printStackTrace();
        }
        } else if(areaChoices[1] == 2){
            System.out.println("This is a city. There are alot of people.");
            cityArea();
        } else if(areaChoices[1] == 3){
            System.out.println("Progressing to next area...");
            magmaArea();
        } else if(areaChoices[1] == 4){
            System.out.println("Returning to main menu.");
            GameMethods.menuCall();
        }
}

public static void magmaArea(){
    GameMethods.space();
    System.out.println("Area 3: Magma Pool. What would you like to do here?: ");
    System.out.println("1 - Save the game?");
    System.out.println("2 - View a description of the area?");
    System.out.println("3 - Progress to the next area?");
    System.out.println("4 - Exit to the main menu?");

    areaChoices[2] = input.nextInt();

    if(areaChoices[2] == 1){
        try{
            FileWriter saveFile3 = new    FileWriter("C:\\Users\\Yemi\\Desktop\\TheGame\\SaveFiles\\TextSave1.txt");
            areaMarkers[2] = 1;
            saveFile3.write(areaMarkers[2]);
            System.out.println("Game has been saved successfully.");
            saveFile3.close();
            magmaArea();
        } catch(IOException ex){
            ex.printStackTrace();
        }
    } else if(areaChoices[2] == 2){
        System.out.println("A blazing pool of molten rock. Very hot.");
        magmaArea();
    } else if(areaChoices[2] == 3){
        System.out.println("Progressing to next area.");
        hospitalArea();
    } else if(areaChoices[2] == 4){
        System.out.println("Returning to main menu.");
        GameMethods.menuCall();
    }
}

public static void hospitalArea(){
    GameMethods.space();
    System.out.println("Area 4: Hospital. What would you like to do here?: ");
    System.out.println("1 - Save the game?");
    System.out.println("2 - View a description of the area?");
    System.out.println("3 - Exit to the main menu.");

    areaChoices[3] = input.nextInt();

    if(areaChoices[3] == 1){
        try
            {
            FileWriter saveFile4 = new FileWriter("C:\\Users\\Yemi\\Desktop\\TheGame\\SaveFiles\\TextSave1.txt");
            areaMarkers[3] = 1;
            saveFile4.write(areaMarkers[3]);
            System.out.println("Game has been successfully saved.");
            saveFile4.close();
            hospitalArea();
        } catch(IOException ex){
            ex.printStackTrace();
        }
    } else if(areaChoices[3] == 2){
        System.out.println("A disused, abandoned hospital. It has an eerie feel     about it.");
        hospitalArea();
    } else if(areaChoices[3] == 3){
        System.out.println("Returning to the main menu...");
        GameMethods.menuCall();
    }
}
} 
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:504)
    at java.lang.Integer.parseInt(Integer.java:527)
    at GameMethods.sChoice(GameMethods.java:59)
    at Choices.main(Choices.java:32)