【问题标题】:How to extract ONLY words from a txt file in Java如何从 Java 中的 txt 文件中仅提取单词
【发布时间】:2015-12-14 07:08:43
【问题描述】:

所以我必须从文本文件中提取数据。

文本文件是这样设置的。

3400      Moderate
310       Light
etc.

我需要提取数字,将它们存储在一个数组和字符串中,然后将它们存储在另一个数组中,这样我就可以根据数组中写入的内容对数字进行计算,然后将其输出到文件中。我已经完成了最后一部分,当我从 txt 中提取数据时,我只是不知道如何将整数与字符串分开。文件。

这是我现在所拥有的,但它只是将 int 和单词提取为 String

import java.io.*;
import java.util.*;

public class HorseFeed {
public static void main(String[] args){

    Scanner sc = null;
    try {
        sc = new Scanner(new File("C:\\Users\\Patric\\Desktop\\HorseWork.txt"));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    List<String> lines = new ArrayList<String>();
    while (sc.hasNextLine()) {
      lines.add(sc.nextLine());
    }

    String[] arr = lines.toArray(new String[0]);

    for(int i = 0; i< 100; i++){
        System.out.print(arr[i]);
    }

}

}

【问题讨论】:

  • 一个解决方案是使用正则表达式:str.matches("[a-zA-Z]+");.. 还有很多其他解决方案,如果您有问题,请尝试考虑一个并发布。
  • 在空格上分割每一行(google for "how to split a String in Java")。第一个元素是 int,第二个是 word。
  • 文件是制表符分隔的,还是那些空格?每行可以有多个单词吗?

标签: java arrays


【解决方案1】:

String 类中使用split(String regex)。设置正则表达式以搜索空格或数字。它将返回一个包含单词的String[]

如果您逐行分析,则需要另一个String[],在其中添加新行中的所有单词。

【讨论】:

    【解决方案2】:

    请按照代码进行操作。

    import java.io.*;
    import java.util.*;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class HorseFeed {
    
        public static void main(String[] args) throws FileNotFoundException, IOException {
    
            List<String> lineList = new ArrayList<String>();
            BufferedReader br = new BufferedReader(new FileReader(new File("C:\\Users\\Patric\\Desktop\\HorseWork.txt")));
                String line;
                while ((line = br.readLine()) != null) {
                    Pattern pattern = Pattern.compile("[0-9]+");
                    Matcher matcher = pattern.matcher(line);
                    if( pattern.matcher(line).matches()){  
                        while(matcher.find()){
                            lineList.add(matcher.group());
                        }
    
                    }
                }
            }
    
        }
    

    这里的 lineList 包含你的整数。

    【讨论】:

      【解决方案3】:

      这应该可行:

      import java.io.*;
      import java.util.*;
      
      public class HorseFeed {
      
          public static void main(String[] args) throws FileNotFoundException {
              List<Integer> intList = new ArrayList<Integer>();
              List<String> strList = new ArrayList<String>();
      
              Scanner sc = new Scanner(new File("C:\\Users\\Patric\\Desktop\\HorseWork.txt"));
              while (sc.hasNextLine()) {
                  String line = sc.nextLine();
                  String[] lineParts = line.split("\\s+");
                  Integer intValue = Integer.parseInt(lineParts[0]);
                  String strValue = lineParts[1];
                  intList.add(intValue);
                  strList.add(strValue);
              }
      
              System.out.println("Values: ");
              for(int i = 0; i < intList.size(); i++) {
                  System.out.print("\t" + intList.get(i) + ": " + strList.get(i));
              }
          }
      
      }   
      

      【讨论】:

        【解决方案4】:

        首先提取文件的所有文本并将其存储到 String 中。然后使用带有模式的字符串类的 replaceall 方法从中删除数字。

        例子:

        String fileText = new String("welcome 2 java");
        ss = fileText.replaceAll("-?\\d+", "");
        System.out.println(ss);
        

        【讨论】:

          猜你喜欢
          • 2013-06-23
          • 1970-01-01
          • 2017-04-03
          • 1970-01-01
          • 1970-01-01
          • 2018-05-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多