Java 分析错误时到达文件结尾
我用Java创建了此代码,但出现以下错误: “分析错误时到达文件末尾”。 有人能看一下吗Java 分析错误时到达文件结尾,java,parsing,compilation,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")
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;
}
import java.io.*;
导入java.util.Scanner;
公共类解析测试{
公共静态void main(字符串args[])引发IOException{
Set positive=loadDictionary(“PositiveWordsDictionary”);
Set negative=loadDictionary(“NegativeWordsDictionary”);
File File=新文件(“fileforparsing”);
BufferedReader br=新的BufferedReader(新的InputStreamReader(新文件InputStream(文件)));
扫描仪sc=新扫描仪(br);
字符串字;
长正计数=0;
长负计数=0;
while(sc.hasNext()){
word=sc.next();
if(正.包含(字)){
System.out.println(“发现阳性”+阳性计数+”:“+字);
正计数++;
}
if(否定。包含(单词)){
System.out.println(“发现负数”+正数+”:“+字);
否定计数++;
}
}
br.close();
}
公共静态集loadDictionary(字符串文件名)引发IOException{
Set words=新HashSet();
文件=新文件(文件名);
BufferedReader br=新的BufferedReader(新的InputStreamReader(新文件InputStream(文件)));
扫描仪sc=新扫描仪(br);
while(sc.hasNext()){
添加(sc.next());
}
br.close();
返回单词;
}
我已检查了大括号错误,但没有任何帮助。您缺少大括号。您的代码应该是这样的:
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;
}
}
import java.io.*;
导入java.util.Scanner;
公共类解析测试
{
公共静态void main(字符串args[])引发IOException
{
Set positive=loadDictionary(“PositiveWordsDictionary”);
Set negative=loadDictionary(“NegativeWordsDictionary”);
File File=新文件(“fileforparsing”);
BufferedReader br=新的BufferedReader(新的InputStreamReader(新文件InputStream(文件)));
扫描仪sc=新扫描仪(br);
字符串字;
长正计数=0;
长负计数=0;
while(sc.hasNext())
{
word=sc.next();
if(正.包含(字))
{
System.out.println(“发现阳性”+阳性计数+”:“+字);
正计数++;
}
if(否定。包含(单词))
{
System.out.println(“发现负数”+正数+”:“+字);
否定计数++;
}
}
br.close();
}
公共静态集loadDictionary(字符串文件名)引发IOException
{
Set words=新HashSet();
文件=新文件(文件名);
BufferedReader br=新的BufferedReader(新的InputStreamReader(新文件InputStream(文件)));
扫描仪sc=新扫描仪(br);
while(sc.hasNext())
{
添加(sc.next());
}
br.close();
返回单词;
}
}
将括号排成一行,以便阅读。在末尾添加一个
}
。似乎不见了。你又少了一个右括号。。。再次检查}
更好的缩进代码可能有助于复制