繁体   English   中英

如何从输入文件中提取小数?

[英]How to extract decimal number from input file?

我是Java的新手,目前正从事一些学校作业。 我的程序正在读取带有行的输入文件。 如果我的输入文件中有一行

“奥尔维尔的英亩,114.8 43801”

我必须将文本,double,integer分成单独的部分。 我已经完成了名称部分,但卡在了数字部分。

我如何只提取114.8,然后提取43801? 我一次需要一个数字。 最后,我需要将114.8分为43801。然后进入下一行。

 Scanner fileInput = new Scanner(inFile);
 fileInput.useDelimiter( "\\D+" );
 double i=0;
 while ( fileInput.hasNextDouble() ){
 i=fileInput.nextDouble(); // get int
 System.out.println(i);
 }

我尝试了这个,但它打印出114.0 8.0 43801.0

如您所见,十进制数字不正确,我一次只需要一个数字。

考虑输入“ 114.8 43801”,您需要使用空格作为分隔符。 通过使用'\\ D +'作为分隔符,您可以按照Java文档将字符串拆分为任何非数字: https//docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern。 html

因此,您将在'114.8'点分裂。

尝试使用默认的空格分隔符(不要设置一个..),它应该可以工作。

您应该使用正则表达式。 样式的文档

String raw="Orville's Acres, 114.8 43801";
Pattern p=Pattern.compile("\\d+.?[\\d]*");
Matcher m=p.matcher(raw);
while(m.find()){
    System.out.println(m.group());
}

输出:

114.8 43801

之所以将8作为一个单独的token是因为您将delimiter设置为Vincent指出的'\\ D +'。 可以将delimiter视为您指定的规则,以了解如何将String的行分成不同的tokens ,其中标记就像是一块。

在不指定您在上面设置的定界符的情况下,即使用默认定界符,您将用' ', '\\t', '\\n'等空白将String分隔开。 这意味着您将可以通过调用scanner.getNext()将这些标记作为字符串值获取:

{Orville's}, {Acres,}, {114.8}, {43801}

并且,如果您了解输入数据的格式,则可以使用Integer.parse(string) Double.parse(string) 这些将解释字符串值中的数字并将其返回给您。 但是,请注意,如果传递给这些方法的字符串参数的格式不是parse()方法所期望的格式(例如: string = "One"string = myId123 ,其中包括不是数字的东西),您将得到格式异常,您的程序将崩溃。

您说您已经完成了姓名部分; 我会假设你能够得到以下line的一部分。 假设您了解输入数据的格式,那么您就会知道双精度数带有“。”。 在令牌中。 因此,您可以尝试以下代码:

String line = "114.8 43801";
while (scanner.hasNext())
{
    String token = scanner.next();

    if (token.contains("."))    // return true if the string has "." in it
    {
        doubleToken = Double.parseDouble(token);
        System.out.println(doubleToken);    // prints "114.8"
    }
    else
    {
        intToken = Integer.parseInt(token);
        System.out.println(intToken);       // prints "43801"
    }
}

假设文件中各行的格式保证为:

String Double Integer

或名称中包含两个单词时:

String String Double Integer

您可以将各行分成三个部分,然后从那里进行解析。 当前,您似乎正在集中精力一次将单个零件从扫描仪中取出,以便可以使用整条生产线。

Scanner fileInput = new Scanner(inFile);
String name = "";
double d = 0;
int i = 0;

while (fileInput.hasNextLine()) {
    String line = fileInput.nextLine(); // get the entire line
    String[] parts = line.split(" ");   // split into its three parts

    if (parts.length == 3) {
        name = parts[0];                    // the first part is the name
        d = Double.valueOf(parts[1]);       // the second part is the double
        i = Integer.valueOf(parts[2]);      // the third part is the integer
    } else {
        name = parts[0] + " " + parts[1];   // the name is the first two parts
        d = Double.valueOf(parts[2]);       // the third part is the double
        i = Integer.valueOf(parts[3]);      // the fourth part is the integer
    }
    System.out.println("Got name: " + name + ", double: " + d + ", int: " + i);
}

这是一个非常有限的示例,但是根据示例输入,它可以工作。 如果需要,它可以扩展为使用名称包含任意数量单词的行。

最简单的方法是:1)使用BufferedReader以String的形式读取整行2)以'space'作为分隔符来拆分字符串,它将返回String []数组3)for(array中的每个元素)3.1)print( Double.parseDouble(element))

您只需将正则表达式更改为

\\\\d+.?[\\\\d]*

暂无
暂无

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

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