解析错误时达到文件结尾

时间:2015-09-28 19:30:17

标签: java parsing compilation

我用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;

}

我检查过花括号错误,但没有任何帮助。

1 个答案:

答案 0 :(得分:0)

你错过了大括号。这就是您的代码应该是这样的:

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

排列括号,使其更容易阅读。