我想要做的是读取一个.java文件,并挑选出所有标识符并将它们存储在列表中。我的问题是.split()方法。如果按原样运行此代码,则将获得ArrayOutOfBounds,但是如果将分隔符从“”更改为。对于其他任何事情,该代码都有效。但是我需要用“。”解析的行。所以还有另一种方法可以做到这一点吗?
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class MyHash {
private static String[] reserved = new String[100];
private static List list = new LinkedList();
private static List list2 = new LinkedList();
public static void main (String args[]){
Hashtable hashtable = new Hashtable(997);
makeReserved();
readFile();
String line;
ListIterator itr = list.listIterator();
int listIndex = 0;
while (listIndex < list.size()) {
if (itr.hasNext()){
line = itr.next().toString();
//PROBLEM IS HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
String[] words = line.split("."); //CHANGE THIS AND IT WILL WORK
System.out.println(words[0]); //TESTING TO SEE IF IT WORKED
}
listIndex++;
}
}
public static void readFile() {
String text;
String[] words;
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader("MyHash.java")); //NAME OF INPUT FILE
} catch (FileNotFoundException ex) {
Logger.getLogger(MyHash.class.getName()).log(Level.SEVERE, null, ex);
}
try {
while ((text = in.readLine()) != null){
text = text.trim();
words = text.split("\\s+");
for (int i = 0; i < words.length; i++){
list.add(words[i]);
}
for (int j = 0; j < reserved.length; j++){
if (list.contains(reserved[j])){
list.remove(reserved[j]);
}
}
}
}
}
}
慕神8447489
慕码人2483693
相关分类