【问题标题】:JAVA parsing data from txt fileJAVA从txt文件中解析数据
【发布时间】:2017-10-08 07:47:18
【问题描述】:

我一直在尝试从文本文件中分离值。这些值不是制表符分隔而是变化的,我试图将它们放入不同的数组中。文本文件示例如下所示。

1908 1 5.0 -1.4 21 --- 29.7 1908 2 7.3 1.9 8 --- 71.9 1908 3 6.2 0.3 13 --- 101.4 1908 4 8.6 2.1 5 --- 128.6 1908 5 15.8 7.7 0 --- 180.4 1908 6 17.7 8.7 0 --- 196.9 1908 7 18.9 11.0 0 --- 196.1 1908 8 17.5 9.7 0 --- 187.2 1908 9 16.3 8.4 0 --- 99.5 1908 10 14.6 8.0 0 --- 56.1 1908 11 9.6 3.4 6 --- 28.4 1908 12 5.8 0.0 13 --- 10.3 1909 1 5. 11 --- 35.6 1909 2 5.5 -0.3 18 --- 49.9 1909 3 5.6 -0.3 17 --- 58.7 1909 4 12.2 3.3 3 --- 188.9 1909 5 14.7 4.8 2 --- 216.8 1909 6 15.0 7.5 0 --- 139.5 1909 7 17.3 10.8 0 --- 151.2 1909 8 18.8 10.7 0 --- 167.5 1909 9 14.5 8.1 0 --- 74.4 1909 10 12.9 6.9 3 --- 101.5

我现在将数据拆分成的数组只是年份;然后,我将为剩余的数据集创建代码。

数组会是这样的:

yyyy [0] 1908
     [1] 1908
ect

public class test {
    private static String removeCharacters(String string){
        string = string.replace("*", "");
        string = string.replace("#", "");
        string = string.replace("---", "");
        return string;
    }


    public static void main(String[] args) throws IOException {
        int i = 0;
        double test[]= new double [1200];
        BufferedReader testFile = new BufferedReader(new FileReader("historical_weather_data/sources/bradforddata.txt"));
        String line;

        testFile.readLine();
        testFile.readLine();
        testFile.readLine();
        testFile.readLine();
        testFile.readLine();
        testFile.readLine();
        testFile.readLine();

        while((line = testFile.readLine()) != null) { 
            String splits[] = line.split("  ");
            test[i] = Double.parseDouble(removeCharacters(splits[0]));
            i++;
        }
        for(int h=0;h<test.length;++h){
            System.out.println(test[h]);
        }
    }
}

从此代码产生以下错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "1908   1    5.0    -1.4      21         29.7"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at test.main(test.java:32)

任何帮助将不胜感激。

【问题讨论】:

  • 仔细阅读您的异常,您正在尝试将字符串“1908 1 5.0 -1.4 21 29.7”解析为双精度,这会给您带来错误。读取 .txt 文件时出错。
  • 这个问题怎么解决?
  • 你能上传整个文本文件吗?
  • 复制到这里太长了,谷歌驱动链接到文档-drive.google.com/open?id=0B4YNBcGEuF3hT3JsLURIZUdTdFE
  • line.split(" ");用 line.split("\t"); 替换它应该可以解决您的问题。

标签: java arrays file csv parsing


【解决方案1】:

查看链接的文档,这些值不是制表符分隔的,而是使用不同数量的空格。 split 方法使用正则表达式,所以应该可以:

String splits[] = line.split(" +");

【讨论】:

  • 它使用它生成一个空字符串,线程“main”中的异常 java.lang.NumberFormatException: empty String
  • 尝试使用splits[1]。行以空格开头,因此第一次拆分位于行首(空字符串)和第一个数字之间。
  • 明白了!非常感谢!
  • @Jamie 或者,使用这个 String[] newLine = line.trim().split(" +");
  • @SeanVanGorder 我已经完成了所有的程序,谢谢你的帮助,只是为了了解更多的程序,你 + 签名是做什么的?
猜你喜欢
  • 1970-01-01
  • 2014-09-19
  • 2019-04-16
  • 1970-01-01
  • 2020-10-02
  • 2013-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多