【问题标题】:How do I make an array of only Strings from a txt file with Strings and doubles?如何从带有字符串和双打的 txt 文件中创建一个仅包含字符串的数组?
【发布时间】:2016-04-12 23:34:08
【问题描述】:

这是 txt 文件:http://txt.do/5w3em

我只需要字符串(不包括注释掉的字符串)、M 和 B,它们都在文本文件的不同行上,并且没有双打。如何制作一个数组列表来存储它们? 我试过这个

List<String> trainingDatasetStrings = new ArrayList<String>();
    while(inFile.hasNextLine()){
        String line = inFile.nextLine();
        String[] words = line.split(",");
        for(int i = 11; i < words.length; i++)
        trainingDatasetStrings.add(words[i]);
    }

但这无济于事。

【问题讨论】:

  • 好吧,它们看起来都可以转换成Object... 使用这些字段。但是一个简单的scan.nextLine().split(","); 就足以将其拆分

标签: java string file double


【解决方案1】:

你可能想尝试这样的事情:

List<String> trainingDatasetStrings = new ArrayList<String>();
while(inFile.hasNextLine()){
    String line = inFile.nextLine().trim();
    // Skip past blank file lines and file lines that start with "//" 
    if (!line.startsWith("//") && !line.equals("")) {
        String[] words = line.split(",");
        trainingDatasetStrings.add(words[words.length - 1]);
    }
}

当然,如果你想要的只是 B 和 M。

【讨论】:

    猜你喜欢
    • 2021-06-30
    • 2021-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    相关资源
    最近更新 更多