【问题标题】:Reached end of file while parsing error解析错误时到达文件末尾
【发布时间】: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


【解决方案1】:

您缺少一个花括号。你的代码应该是这样的:

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

把你的括号排成一行,这样更容易阅读。

【讨论】:

    猜你喜欢
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多