Java 需要解析一个字符串,以便将每个单独的元素与另一个字符串中的元素进行比较
我试图解析一个文件中的文本,该文件的内容是“这是一个测试,这是一个简单的测试”。我需要解析它,以便将它与另一个文件(字典)进行比较。这是拼写检查程序的一部分。我在实现r.readline和r.split方法时遇到问题Java 需要解析一个字符串,以便将每个单独的元素与另一个字符串中的元素进行比较,java,string,parsing,bufferedreader,Java,String,Parsing,Bufferedreader,我试图解析一个文件中的文本,该文件的内容是“这是一个测试,这是一个简单的测试”。我需要解析它,以便将它与另一个文件(字典)进行比较。这是拼写检查程序的一部分。我在实现r.readline和r.split方法时遇到问题 String fileName2 = "test.txt"; String line2 = null; String[] diction2 = new String [100]; try { // FileReader reads
String fileName2 = "test.txt";
String line2 = null;
String[] diction2 = new String [100];
try {
// FileReader reads text files in the default encoding.
FileReader fileReader2 =
new FileReader(fileName2);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader2 =
new BufferedReader(fileReader2);
int j=0;
while((line = bufferedReader2.readLine()) != null) {
//System.out.println(line);
diction2[j]=line;
System.out.println(diction2[j]);
j++;
//r.readline(line);
//delimiter
//r.split
//need to parse line to get each individual word out and compare to dictionary
}
// Always close files.
bufferedReader2.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
编辑:
当您这样做时:
for(String word : words){
//Do some action
}
这意味着它将逐个获取“words”数组中的每个字符串,将其存储在“word”中,并为其执行“dosomeaction”
这与写作是一样的:
for(int i = 0; i < words.length; i++){
String word = words[i];
//Do something
}
你已经在读这行了,难道你不能:
String fileName2 = "test.txt";
String line = null;
String[] diction2 = new String [100];
try {
// FileReader reads text files in the default encoding.
FileReader fileReader2 =
new FileReader(fileName2);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader2 =
new BufferedReader(fileReader2);
int j=0;
while((line = bufferedReader2.readLine()) != null) {
//System.out.println(line);
diction2[j]=line;
System.out.println(diction2[j]);
j++;
//There you get the list of all the words in the line
String[] words = line.split(" ");
for(String word : words){
//Check your word here
}
}
// Always close files.
bufferedReader2.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
split method在哪里?变量“r”的类型是什么?你有什么问题,例如,它做什么和你希望它做什么?我已经读入了文件,我只需要解析单词,所以它们不是打印在一行上,而是按顺序垂直打印出来,并且能够将每个单词与我的字典文件中的单词进行比较,我从来没有见过字符串单词:words格式,这有什么用?好的,谢谢你,等我有更多时间的时候,我会尝试一下。
String fileName2 = "test.txt";
String line = null;
String[] diction2 = new String [100];
try {
// FileReader reads text files in the default encoding.
FileReader fileReader2 =
new FileReader(fileName2);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader2 =
new BufferedReader(fileReader2);
int j=0;
while((line = bufferedReader2.readLine()) != null) {
//System.out.println(line);
diction2[j]=line;
System.out.println(diction2[j]);
j++;
//There you get the list of all the words in the line
String[] words = line.split(" ");
for(String word : words){
//Check your word here
}
}
// Always close files.
bufferedReader2.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}