为什么我在Java中提交这些错误?

时间:2014-02-25 05:55:03

标签: java

我正在申请这个我所拥有的大项目,它将在几个小时内到期,我的老师在他的网页上有一本名为Blue Pelican的教科书,我总是这么说。我的程序包括用户输入3个数字(整数)来获取文件的输出。但是,当我按照教科书进行操作时,我得到了4个错误,我无法解决:

if (choose == 1) //enemies from the original Kingdom Hearts
        {
        System.out.println("In the orginal Kingdom Hearts, there is only one enemy race, which is the Heartless.");
        System.out.println("Please type the number which corresponds to the Heartless enemy type.");
        System.out.println("1. Pureblood"); //Gives the display for the user to know she can choose to view Pureblood Heartless.
        System.out.println("2. Emblum"); //Gives the display for the user to know she can choose to view Emblem Heartless.
        System.out.println("3. Special Heartless");
        int heartless_1 = kH.nextInt();
        if (heartless_1 == 1) //gives the chioce to select the Heartless enemy type
        {
            Scanner kH1 = new Scanner(new File("KH_Pureblood_Heartless.txt"));
            int maxIndex = -1; //So that when it's incremented below, the first index is 0.

            String kh1Purebloods = new String[10000]; //To be safe, more than what's needed is declared just in case.

            while (kh1.hasNext())
            {
                maxIndex++;
                kh1Purebloods[maxIndex] = kH1.nextLine();
            }

            kH1.close();

            for (int a = 0; a <= maxIndex; a++)
            {
                System.out.println(kh1Purebloods[a]);
            }
        }   

我得到的错误包括:

E:\3 IB Computer Science SL\Dossier Computer Science IA The Solution\Product\Kingdom_Hearts.java:45: incompatible types
found   : java.lang.String[]
required: java.lang.String
                String kh1Purebloods = new String[10000]; //To be safe, more than what's needed is declared just in case.
                                       ^


E:\3 IB Computer Science SL\Dossier Computer Science IA The Solution\Product\Kingdom_Hearts.java:47: cannot find symbol
symbol  : method hasNext()
location: class kh1
                while (kh1.hasNext())
                          ^


E:\3 IB Computer Science SL\Dossier Computer Science IA The Solution\Product\Kingdom_Hearts.java:50: array required, but java.lang.String found
                    kh1Purebloods[maxIndex] = kH1.nextLine();
                                 ^


E:\3 IB Computer Science SL\Dossier Computer Science IA The Solution\Product\Kingdom_Hearts.java:57: array required, but java.lang.String found
                    System.out.println(kh1Purebloods[a]);
                                                    ^
4 errors

Process completed.

这是我用作参考的Blue Pelican Textbook(参考24-3):

1 个答案:

答案 0 :(得分:3)

String kh1Purebloods = new String[10000] kh1Purebloods是一个String,但您正在尝试为其分配一个数组。从第57行开始,您可以将其作为数组访问,它应该是:String[] kh1Purebloods = new String[10000]

您使用kh1但扫描程序为kH1(Java区分大小写)