我的java程序有问题。我必须从一个文件中读取行,这些行的形式是。
1#the^cat#the^dog#the^bird#^fish#bear.
2#the^cat#the^dog#the^bird#^fish#bear
并全部打印,在我的GUI中的文本字段接受 "#"和"^"。"^"必须出现在没有文章的情况下。例如^fish,我必须打印成^fish,但是^dog我必须打印成dog.到目前为止,我可以阅读和打印文本字段中的行,但是我找不到一种方法来跳过单词之间的"^"。
这是我的代码。
try {
FileReader file = new FileReader("C:\\Guide.txt");
BufferedReader BR = new BufferedReader(file);
boolean eof = false;
int i=0;
while (!eof) {
String line = BR.readLine();
if (line == null)
eof = true;
else {
i++;
System.out.println("Parsing line "+i+" <"+line+">");
String[] words = line.split("#");
if (words.length != 7) continue;
number=words[0];
onomastiki=words[1];
geniki=words[2];
aitiatiki=words[3];
klitiki=words[4];
genos=words[5];
Region=words[6];
E = new CityEntry(number,onomastiki,geniki,
aitiatiki,klitiki,
genos,Region);
Cities.add(E);
}
你可以试试这样的方法。
FileReader file = new FileReader("C:\\\\Users\\\\aq104e\\\\Desktop\\\\text");
BufferedReader BR = new BufferedReader(file);
boolean eof = false;
int i = 0;
while (!eof) {
String line = BR.readLine();
if (line == null)
eof = true;
else {
i++;
System.out.println("Parsing line " + i + " <" + line + ">");
String[] words = line.split("#");
for (int j = 0; j < words.length; j++) {
if(words[j].contains("^")) {
if(words[j].indexOf("^") == 0) {
// write your code here
//This is case for ^fish
}else {
// split using ^ and do further manipulations
}
}
}
}
}
让我知道这对你是否有效。
这是要去工作,但它不是最好的方式)
foreach(String word : words){
if(word.contains"the"){
word.replace("^"," ");
}
}