【发布时间】:2015-09-29 05:30:35
【问题描述】:
我在 Java 中创建了这段代码,但出现以下错误: “解析错误时到达文件末尾”。 有人可以看看吗?
import java.io.*;
import java.util.Scanner;
public class ParseTest {
public static void main(String args[]) throws IOException {
Set<String> positive = loadDictionary("PositiveWordsDictionary");
Set<String> negative = loadDictionary("NegativeWordsDictionary");
File file = new File("fileforparsing");
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
Scanner sc = new Scanner(br);
String word;
long positiveCount = 0;
long negativeCount = 0;
while (sc.hasNext()) {
word = sc.next();
if (positive.contains(word)) {
System.out.println("Found positive "+positiveCount+":"+word);
positiveCount++;
}
if (negative.contains(word)) {
System.out.println("Found negative "+positiveCount+":"+word);
negativeCount++;
}
}
br.close();
}
public static Set<String> loadDictionary(String fileName) throws IOException {
Set<String> words = new HashSet<String>();
File file = new File(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
Scanner sc = new Scanner(br);
while (sc.hasNext()) {
words.add(sc.next());
}
br.close();
return words;
}
我检查了花括号错误,但没有任何帮助。
【问题讨论】:
-
在末尾添加
}。好像不见了。 -
你又少了一个右花括号...再次检查
}更好的缩进代码可能会有所帮助
标签: java parsing compilation