【问题标题】:Using Sort Arrays and finding the growth rate in states使用排序数组并查找各州的增长率
【发布时间】:2013-04-25 01:40:02
【问题描述】:

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

      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;

            }
        }
    }
}

为什么会出现这个错误?

【问题讨论】:

  • 什么错误?您是否希望每个人都编译您的代码以查看错误是什么以及在哪里?你为什么不直接告诉我们?
  • 要扩展 Johns 的评论,最好发布堆栈。
  • 我告诉过你错误,它是输入不匹配异常,我标记了错误的位置。我刚刚添加了一个文件示例。

标签: java arrays sorting methods


【解决方案1】:

正如@Pescis 的回答所说,例外是您尝试使用nextInt 获取的令牌不是int 的有效表示。要么包含意外字符,要么超出范围。

如果您向我们展示了异常的消息字符串,以及完整的堆栈跟踪,则更容易诊断。没有这个,我们就只能靠猜测了。

猜测 #1:此时您正在读取的实际输入数据文件中有问题。

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

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

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


评论:

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

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

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

【讨论】:

  • 谢谢你,我要做的就是删除文件并创建一个新文件!
【解决方案2】:

来自javadoc of Scanner.nextInt()

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

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

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2014-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-07
        • 1970-01-01
        • 2021-11-02
        相关资源
        最近更新 更多