【问题标题】:simple array table, unknown Error简单数组表,未知错误
【发布时间】:2017-09-23 19:58:49
【问题描述】:

我正在尝试在 ecplise 中创建一个简单的数组表。 3 列 3 行。第 1 和第 2 列是字符串,第 3 列是双精度数。如果他犯了错误,我还希望用户可以选择重写当前行。

很简单吧?我收到一个我不明白的错误。有人可以向我解释一下吗?这是代码:

import java.util.Scanner;

public class exercise923 
{
    public static void main(String args[]) 
    {
        int counter = 0, check; //these are numbers used for the loops
        String column1[] = new String[3]; //these are data for the table
        String column2[] = new String[3];
        double column3[] = new double[3];
        Scanner Scan = new Scanner(System.in);

        //this do while loop allows the user to enter data for the table
        do {
            System.out.println("type values for columns 1, 2, and 3 of line: " + counter);
            column1[counter] = Scan.nextLine();
            column2[counter] = Scan.nextLine();
            column3[counter] = Scan.nextDouble();

            //after typing out the data for this line, user can type 1 to go to the next line, or 2 to rewrite this line
            System.out.println("type 1 to continue, 2 to rewrite");
            check = Scan.nextInt();
            if (check==1) {
                counter++;
            }
       } while (counter<3);

        //once user is finished typing out all the data for the 3 lines
        //of 3 columns, the table gets printed out.
        counter=0;
        System.out.println("your finished table:");

        do {
            System.out.print(column1[counter] + "\t");
            System.out.print(column2[counter] + "\t");
            System.out.println(column3[counter]);
            counter++;
        } while (counter<3);
    } // main   
} // class

当我运行它时,我得到:

type values for columns 1, 2, and 3 of line: 0
jack
bob
3
type 1 to continue, 2 to rewrite
2
type values for columns 1, 2, and 3 of line: 0
dale
sally
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at exercise923.main(exercise923.java:21)

【问题讨论】:

标签: java arrays string if-statement do-while


【解决方案1】:

使用时:

check = Scan.nextInt();

在循环结束时,它读取int,但不是行尾。在下一个循环中

column1[counter] = Scan.nextLine();

将读取行尾,因此,您输入的第一个值(在您的示例中为dale)将被读取:

column2[counter] = Scan.nextLine();

以及下一行:

column3[counter] = Scan.nextDouble();

将无法读取Double,因为您输入了 (sally)。

一种可能的解决方案是在check = Scan.nextInt(); 之后添加一个Scan.nextLine();

do    {
        System.out.println("type values for columns 1, 2, and 3 of line: " + counter);
        column1[counter] = Scan.nextLine();
        column2[counter] = Scan.nextLine();
        column3[counter] = Scan.nextDouble();
        //after typing out the data for this line, user can type 1 to go to the next line, or 2 to rewrite this line
        System.out.println("type 1 to continue, 2 to rewrite");
        check = Scan.nextInt();
        Scan.nextLine();
        if (check==1) {counter++;}else {}
}while (counter<3);

【讨论】:

  • 不客气。请@sculpter,不要忘记将答案标记为您问题的解决方案。
【解决方案2】:

您可以使用验证输入来检查下一个输入是否为双精度。如果它只是 double 则继续并将其分配给 double 变量,否则不要

if(scan.hasNextDouble()) 
{
   id = keyboard.nextDouble();
}

【讨论】:

  • 不鼓励仅使用代码的答案,因为它们没有解释如何解决问题中的问题。考虑更新您的答案以解释它的作用以及它如何解决问题 - 这不仅有助于 OP,还有助于其他有类似问题的人。请查看How do I write a good answer
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-01
相关资源
最近更新 更多