如何在Java中仅从txt文件中提取单词

如何在Java中仅从txt文件中提取单词,java,arrays,Java,Arrays,所以我必须从文本文件中提取数据 文本文件是这样设置的 3400 Moderate 310 Light etc. 我需要提取数字,将它们存储在一个数组中,将字符串存储在另一个数组中,这样我就可以根据数组中写入的内容对数字进行计算,然后将其输出到一个文件中。我已经记下了最后一部分,当我从txt中提取数据时,我不知道如何将int和字符串分开。文件 这里是我现在所拥有的,但它只是将int和单词提取为字符串 import java.io.*; import java.util.*;

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

文本文件是这样设置的

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

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

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]);
    }

}

}
import java.io.*;
导入java.util.*;
公共级饲料{
公共静态void main(字符串[]args){
扫描仪sc=空;
试一试{
sc=新扫描仪(新文件(“C:\\Users\\Patric\\Desktop\\HorseWork.txt”);
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
列表行=新的ArrayList();
while(sc.hasNextLine()){
line.add(sc.nextLine());
}
String[]arr=lines.toArray(新字符串[0]);
对于(int i=0;i<100;i++){
系统输出打印(arr[i]);
}
}
}
String
类中使用
split(String regex)
。设置正则表达式以搜索空格或数字。它将返回一个包含单词的
字符串[]

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

plz,请按照代码进行操作

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());
                    }

                }
            }
        }

    }
import java.io.*;
导入java.util.*;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
公共级饲料{
公共静态void main(字符串[]args)抛出FileNotFoundException、IOException{
List lineList=new ArrayList();
BufferedReader br=new BufferedReader(新文件阅读器(新文件(“C:\\Users\\Patric\\Desktop\\HorseWork.txt”));
弦线;
而((line=br.readLine())!=null){
Pattern=Pattern.compile(“[0-9]+”);
匹配器匹配器=模式匹配器(线);
if(pattern.matcher(line.matches()){
while(matcher.find()){
lineList.add(matcher.group());
}
}
}
}
}
这里的行列表包含整数。

这应该可以:

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));
        }
    }

}   
import java.io.*;
导入java.util.*;
公共级饲料{
公共静态void main(字符串[]args)引发FileNotFoundException{
List intList=new ArrayList();
List strList=new ArrayList();
Scanner sc=新扫描仪(新文件(“C:\\Users\\Patric\\Desktop\\HorseWork.txt”);
while(sc.hasNextLine()){
字符串行=sc.nextLine();
字符串[]lineParts=line.split(\\s+);
整数intValue=Integer.parseInt(lineParts[0]);
字符串strValue=lineParts[1];
intList.add(intValue);
strList.add(strValue);
}
System.out.println(“值:”);
对于(inti=0;i
首先提取文件的所有文本并将其存储到字符串中。然后使用带有模式的string类的replaceall方法从中删除数字。 范例-

String fileText=新字符串(“欢迎使用java”)

fileText=fileText.replaceAll(“-?\d+”,”)


系统输出打印LN(ss)

一种解决方案是使用正则表达式:
str.matches(“[a-zA-Z]+”)。。还有很多其他的解决方案,如果你有问题,请试着考虑一个,然后发布它。在空格上拆分每一行(谷歌的“如何在Java中拆分字符串”)。第一个元素是int,第二个是word。文件选项卡是分开的,还是那些空格?每行可以有多个单词吗?