【问题标题】:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at Generator.main(Generator.java:35)线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 3 在 Generator.main(Generator.java:35)
【发布时间】:2014-11-09 19:54:51
【问题描述】:
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Array;
import java.util.Scanner;

public class Generator {

    public static void main(String[] args) throws FileNotFoundException {

        int scorehome = 0;
        int scoreaway = 0;
        int invalid = 0;
        int goals = 0;
        int valid = 0;

        Scanner scanner = new Scanner(new File("Results.txt")); // create a scanner which scans from a file

        String line;    // stores the each line of text read from the file

        while ( scanner.hasNext() ) {
            line = scanner.nextLine();  // read the next line of text from the file

            //split the line
            String [] elements = line.split(":");

            //System.out.println("Element " + (i+1) + " was : " + elements[i]);
            if (elements.length < 4) {                  
                String home = elements[0].trim();
                String away = elements[1].trim();
                String homescore = elements[2].trim();
                String awayscore = elements[3].trim();

                boolean homescoreVal = false;
                boolean awayscoreVal = false;
                boolean homenameVal = false;
                boolean awaynameVal = false;

                try {   // "try" is a special statement which allows us to deal with "exceptions"
                    scorehome = Integer.parseInt(homescore);    // attempt to convert the String into an Integer type value
                    homescoreVal = true;
                } catch (NumberFormatException e) {
                    homescoreVal = false;
                }
                try {
                    scoreaway = Integer.parseInt(awayscore);    // attempt to convert the String into an Integer type value
                    awayscoreVal = true;
                } catch (NumberFormatException e) {
                    homescoreVal = false;
                }

                if (home.length() <= 1) {
                    homenameVal = false;
                } else {
                    homenameVal = true;
                }

                if (away.length() <= 1) {
                    awaynameVal = false;
                } else {
                    awaynameVal = true;
                }

                if (homescoreVal == true && awayscoreVal == true
                        && homenameVal == true && awaynameVal == true){ 

                    System.out.println(home + " [" + scorehome + "] | "
                            + away + " [" + scoreaway + "]\r");

                    goals = (scorehome + scoreaway) + goals;

                    valid = 1 + valid;
                } else {
                    invalid = 1 +invalid;
                }

            } else {
                invalid = 1 + invalid;
            }
        }

        System.out.println("\rValid match was " + valid
                + ", total goals scored was " + goals);
        System.out.println("Invalid match count was " + invalid + ".");

        System.out.println("\nEOF");    // Output and End Of File message.

    }
}

我似乎遇到了资源泄漏,说我的扫描仪从未关闭,我的import java.lang.reflect.Array; 从未使用过。关于如何解决这两个问题以及为什么会发生的任何想法?

【问题讨论】:

  • 我不明白你在说什么?
  • 第 35 行是哪一行?我们大多数人不会将您的代码复制粘贴到文本编辑器中。
  • 上面源代码中哪一行是源代码中的第35行作为错误表示错误在第35行。第35行是哪一行?
  • String awayscore = elements[3].trim();第 35 行对不起

标签: java indexoutofboundsexception


【解决方案1】:

你可能想改变

        if (elements.length < 4) {
            String home = elements[0].trim();
            String away = elements[1].trim();
            String homescore = elements[2].trim();
            String awayscore = elements[3].trim();

        if (elements.length >= 4) {
            String home = elements[0].trim();
            String away = elements[1].trim();
            String homescore = elements[2].trim();
            String awayscore = elements[3].trim();

因为您依赖至少 4 个元素存在于 elements 数组中(当您尝试访问 elements[3] 时)。

【讨论】:

  • 程序需要确保恰好有 4 个元素,如果没有,则不会显示结果
  • @JackHumphreys 如果您只想要 4 个元素,请将条件更改为 elements.length == 4
【解决方案2】:

完成 Scanner 实例后,调用scanner.close()。只需擦除 Array 导入;你不使用它,所以它是不必要的。

【讨论】:

    猜你喜欢
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 2012-10-29
    • 1970-01-01
    • 2012-08-13
    相关资源
    最近更新 更多