0

我无法弄清楚为什么会出现不匹配异常,一切正常,直到我在计算机上打开它。我必须把人口普查文件读进去,我这样做了,我找到了增长率,但唯一的问题似乎是这个不匹配异常,我不明白为什么会出现不匹配错误,请帮忙。这是文件示例:

      Alabama,4447100
      Alaska,626932
      Arizona,5130632
      Arkansas,2673400

这是程序:

    public static void main(String[] args) throws IOException {
    File f = new File("census2010.txt");
    File ff = new File("census2000.txt");
    if(!f.exists()) {
        System.out.println( "f does not exist ");
    }
    if(!ff.exists()) {
        System.out.println( "ff does not exist ");
    }
    Scanner infile2010 = new Scanner(f);
    Scanner infile2000 = new Scanner(ff);
    infile2010.useDelimiter("[\t|,|\n|\r]+");
    infile2000.useDelimiter("[\t|,|\n|\r]+");
    final int MAX = 60;
    int[] pop10 = new int[MAX];
    int[] pop00 = new int[MAX];
    String[] statearray10 = new String[MAX];
    String[] statearray00 = new String[MAX];
    double [] growtharray = new double[MAX];
    int fillsize;

    fillsize = fillArrayPop (pop10, statearray10, MAX, infile2010, prw);
    fillsize = fillArrayPop2 (pop00, statearray00, MAX, infile2000, prw); // the mismatch exception occurs here
    growthRate(growtharray, pop10, pop00, fillsize);
    sortarray(growtharray, statearray10, fillsize);
    printarray (growtharray, statearray10, fillsize, prw);


}

public static int fillArrayPop (int[] num, String[] statearray10, int mAX, Scanner infile2010, PrintWriter prw) throws FileNotFoundException{
    int retcnt = 0;
    int pop;
    String astate;
    while(infile2010.hasNext()){
        astate = infile2010.next();
        pop = infile2010.nextInt();
        statearray10[retcnt] = astate;
        num[retcnt] = pop;
        retcnt++;
    }
    return (retcnt);
}



public static int fillArrayPop2 (int[] number, String[] statearray00, int mAX, Scanner infile2000, PrintWriter prw) throws FileNotFoundException{
    int retcounts = 0;
    int pop;
    String state;
    while(infile2000.hasNext()){
        state = infile2000.next();
        pop = infile2000.nextInt(); // the mismatch exception occurs here
            statearray00[retcounts] = state;
            number[retcounts] = pop;
            retcounts++;
    }
    return (retcounts);
}

public static void printarray (double[] growth, String[] state, int fillsize, PrintWriter prw){
    DecimalFormat form = new DecimalFormat("0.000");
    for (int counts = 0; counts < fillsize ; counts++){
        System.out.println("For the position ["+counts+"] the growth rate for " + state[counts] + " is " +form.format(growth[counts]));
        prw.println("For the position ["+counts+"] the growth rate for " + state[counts] + " is " + form.format(growth[counts]));
    }
    prw.close();
    return;
}

public static void growthRate (double[] percent, int[] pop10, int[] pop00, int fillsize){
    double growthrate = 0.0;
        for(int count = 0; count < fillsize; count ++){
            percent[count] = (double)(pop10[count]-pop00[count])/ pop00[count];

        }

}

public static void sortarray(double[] percent, String[] statearray10, int fillsize) {

    for (int fill = 0; fill < fillsize - 1; fill = fill + 1) {

        for (int compare = fill + 1; compare < fillsize; compare++) {

            if (percent[compare] < percent[fill]) {
                double poptemp = percent[fill];
                percent[fill] = percent[compare];
                percent[compare] = poptemp;

                String statetemp = statearray10[fill];
                statearray10[fill] = statearray10[compare];
                statearray10[compare] = statetemp;

            }
        }
    }
}

为什么我会收到此错误?

4

4 回答 4

0

来自的javadocScanner.nextInt()

InputMismatchException - 如果下一个标记与 Integer 正则表达式不匹配,或者超出范围

这意味着,当它抛出错误时,您尝试读取的下一个令牌不能被解析为整数。例如,如果令牌中有非数字字母或整数超出范围,即小于 -2,147,483,648 或大于 2,147,483,647,就会发生这种情况。

如果您的文件中没有标记超出范围,则很可能是因为文件的布局不是您对其进行编程的方式。

于 2013-04-25T01:51:19.033 回答
0

如果您在打印数组中取出您的 prw,您的程序应该可以正常运行。DELETE prw.println("对于位置 ["+counts+"] " + state[counts] + " 的增长率是 " + form.format(growth[counts])); 和 DELETE prw.close() 它应该运行。

于 2013-04-25T02:17:15.490 回答
0

正如@Pescis 的回答所说,例外情况是您尝试使用的令牌nextInt不是int. 它包含意外字符,或者超出范围。

如果您向我们展示了异常的消息字符串和完整的堆栈跟踪,则诊断起来会更容易。没有这个,我们不得不求助于猜测。

猜想#1:此时您正在阅读的实际输入数据文件中有一些不好的地方。

猜猜#2:您设置分隔符正则表达式的方式是错误的。您的正则表达式似乎以几乎可以肯定不正确的方式混合了字符类和交替。应该是这样的:

  useDelimiter("[\\t,\\n\\r]+");

(目前尚不清楚修复此问题是否会解决问题......但您的正则表达式仍然是错误的。)


评论:

  1. 您有两种方法fillArrayPopfillArrayPop2并且似乎在做同样的事情。据我所知,唯一的区别是不同的变量名和其他无关紧要的东西。

  2. 您的“解析”方法忽略了文件实际上是行结构的事实。我认为您应该(从概念上)阅读行,然后将行拆分为字段。您当前正在做的是将它们视为一系列交替场。

  3. 理想情况下,您的“解析”应该更健壮。它至少应该尝试诊断预期文件格式与您从文件中实际读取的内容之间的不匹配。(如果我正在写这个,我至少会尝试输出检测到“坏处”的行或行号。)

于 2013-04-25T02:27:22.797 回答
0

如果文件的格式与您在上面描述的完全一样,那么紧接错误上方的 .next() 方法调用实际上会拉动整行(即变量“state”将包含“Alabama,4447100”)。

由于状态名称和人口之间没有空格/分隔符,.next() 读取整行,并且对 .nextInt() 的下一次调用尝试读取下一行(字符串),产生错误。

我建议单独阅读每一行,然后用逗号分隔两条数据。另外,如果方法中没有使用对象,为什么要向方法传递一个 PrintWriter 对象?

于 2013-04-25T03:08:54.877 回答