【问题标题】:Returning a specifc part of each line in a txtfile and storing it into a double返回文本文件中每一行的特定部分并将其存储到双精度
【发布时间】:2019-11-08 21:58:31
【问题描述】:

我想返回每行中包含数字之间的点的部分,这是文本文件中每行的 GPA 部分。

我可以弄清楚如何读取文本文件中的每一行并打印出来。我曾尝试使用.contains.substring 尝试从每一行中捕获我想要的部分,但我不知道该怎么做。

1.wita7557 | william | tang | male | 3.0 | counselor | 5 | database | leader  
2.tosm5791 | tommy | smith | male | 3.5 | mastermind | 5 | designer | leader  
3.drco5905 | drew | collins | male | 3.7 | inspector | 3 | designer | programmer 

所以从上面的代码中我想要一个预期的结果到一个数组中,比如[3.0,3.5,3.7]

【问题讨论】:

  • 使用 line.split("\\|") 在每个 | 处拆分您的行,并在结果数组的第 4 个索引处获取您想要的字符串。
  • 你需要 google 3 件事:如何逐行读取文件?如何在管道字符上拆分字符串?如何修剪字符串?
  • 您需要字符串列表还是双精度列表?

标签: java filereader string-parsing


【解决方案1】:

下面应该给你每行的第 5 个元素(由 '|' 分割):

line.split("\\|")[4] 

【讨论】:

    【解决方案2】:

    示例代码

    Double.valueOf(
    "2.tosm5791 | tommy | smith | male | 3.5 | mastermind | 5 | designer | leader"
    .split("\\|")[4])
    

    【讨论】:

      【解决方案3】:

      完整的程序:

      public static void main(String[] args) {
          try {
              BufferedReader reader = new BufferedReader(new FileReader("C:/Users/user/Desktop/input.txt"));
              List<Double> doubleList = new ArrayList<>();
              String buff[];
              String line = reader.readLine();
              while (line != null) {
                  buff = line.split("\\|");           
                  doubleList.add(Double.parseDouble(buff[4]));
                  line = reader.readLine();
              }
              reader.close();
              System.out.println(doubleList);
          } catch (IOException e) {
              e.printStackTrace();
          }
      
      }
      

      【讨论】:

        【解决方案4】:

        你想做的是:

        这可以通过这种方式使用单个 lambda 来完成:

        List<Double> list = Files.lines(Paths.get("myFile.txt")) // real all lines
            .map(line -> line.split("\\|"))                      // split each line on the | character
            .map(array -> array[4])                              // take the 5th element (index 4)
            .mapToDouble(Double::valueOf)                        // parse them into doubles
            .boxed()                                             // box them to allow collection
            .collect(Collectors.toList());                       // make a list out of them
        

        或者没有 lambda 也一样:

        List<String> lines = Files.readAllLines(Paths.get("myFile.txt"));
        List<Double> list = new ArrayList<>();
        String[] array;
        String stringValue;
        Double doubleValue;
        for(String line : lines) {
            array = line.split("\\|");
            stringValue = array[4];
            doubleValue = Double.valueOf(stringValue);
            list.add(doubleValue);
        }
        

        注意: Files.line() 会抛出 IOException,因此您需要将该代码写入 try/catch/finally 块中

        【讨论】:

          猜你喜欢
          • 2014-04-30
          • 2015-07-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-23
          • 2020-03-07
          • 1970-01-01
          相关资源
          最近更新 更多