简体   繁体   中英

Reading doubles from a .txt file

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM