-1

and im trying to create a simple array table in ecplise. 3 columns with 3 lines. 1st and 2nd columns are strings and 3rd column is doubles. i also want the user to have an option to rewrite the current line if he made a mistake.

very simple right? i'm getting an error that i do not understand. can someone please explain it to me? here is the code:

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

when i run this, i get:

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)
Johnny Dew
  • 829
  • 2
  • 11
  • 28
sculpter
  • 15
  • 2

2 Answers2

0

You can use to verify the input to check whether the next input is double or not. If it is double only then go ahead and assign it to a double variable, otherwise don't

if(scan.hasNextDouble()) 
{
   id = keyboard.nextDouble();
}
Rakesh0502
  • 5
  • 1
  • 4
  • Code-only answers are discouraged because they do not explain how they resolve the issue in the question. Consider updating your answer to explain what this does and how it addresses the problem - this will help not just the OP but others with similar issues. Please review [How do I write a good answer](https://stackoverflow.com/help/how-to-answer) – FluffyKitten Sep 24 '17 at 02:10
0

When you use:

check = Scan.nextInt();

at the end of the loop, it reads the int, but not the end of the line. In the next loop

column1[counter] = Scan.nextLine();

will read the end of the line, so, the first value you enter (dale in your example) will be read by:

column2[counter] = Scan.nextLine();

and the next line:

column3[counter] = Scan.nextDouble();

will fail to read a Double since you entered (sally).

A possible solution would be to a add a single Scan.nextLine(); after 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);
y.luis
  • 1,642
  • 3
  • 20
  • 37
  • You are welcome. Please @sculpter, don't forget to mark the answer as the solution for you question. – y.luis Sep 24 '17 at 20:42