简单数组表,未知错误

时间:2017-09-23 19:58:50

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

我试图在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)

2 个答案:

答案 0 :(得分:0)

您可以使用验证输入来检查下一个输入是否为双倍。如果它只有两倍,那么继续将它分配给一个双变量,否则不要

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

答案 1 :(得分:0)

使用时:

check = Scan.nextInt();

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

column1[counter] = Scan.nextLine();

将读取该行的结尾,因此,您输入的第一个值(示例中为dale)将通过以下方式读取:

column2[counter] = Scan.nextLine();

和下一行:

column3[counter] = Scan.nextDouble();
自您输入(Double将无法读取sally

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

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);