Pregunta

I'm trying to read a .txt file which has a line of 3 elements, the first being a string such as "tri" and then the next two are doubles like 2.3 and 4.2. The problem is, I keep getting a compiler error when I try to read the next double. If anyone can help me out I would greatly appreciate it. This is my problematic code:

            if(order.equals("printAll"))
            {
                printAll();
                if(textReader.hasNext())
                {
                    order = textReader.next();
                    if(textReader.hasNextDouble())
                    {
                        dimension1 = textReader.nextDouble();
                        dimension2 = textReader.nextDouble();  <---**this line**
                    }
                }
            }

The line I pointed at keeps giving me the error:

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 arrayDrawer.main(arrayDrawer.java:88)

Line 88 being the line I pointed to. Sorry if it's vague, I will post more of my code if need be. Any help would be greatly appreciated.

Input for the program:

rec 4.2 4.0
tri 4.9 9.3
tri 2.1 9.9
compAreaAll
printAll 
del 4.9
printAll
¿Fue útil?

Solución

With that input, your program is reading the final "printAll", then "del" as the order number, then "4.9" as a double, then it tries to use nextDouble to read the final "printAll" and fails. The order "del" should be followed by two doubles.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top