简体   繁体   English

从.txt文件读取双打

[英]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. 我正在尝试读取一个.txt文件,该文件包含3个元素,第一个是诸如“ tri”之类的字符串,然后下两个是像2.3和4.2这样的双精度形式。 The problem is, I keep getting a compiler error when I try to read the next double. 问题是,当我尝试读取下一个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. 第88行是我指向的行。 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. 使用该输入,您的程序正在读取最终的“ printAll”,然后读取“ del”作为订单号,然后将“ 4.9”读取为双精度,然后尝试使用nextDouble读取最终的“ printAll”并失败。 The order "del" should be followed by two doubles. 顺序“ del”后应跟两个双打。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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