写了一个Dictionary类,但是在主函数中一声明就出错。部分错误消息如下:.\Dictionary.java:18: <identifier> expected
private HashSet<String> words;
^
.\Dictionary.java:19: <identifier> expected
private HashSet<String> wordsExistThatStartWith;
^
.\Dictionary.java:30: '(' or '[' expected
words = new HashSet<String>();
^
我看了一下,好像Java SDK中本来就有个Dictionary类,是不是跟那个冲突了?下面是代码
import java.io.*;
import java.util.HashSet;public class Dictionary { private HashSet<String> words;
private HashSet<String> wordsExistThatStartWith; private File configurationFile; /**
* Construct the class by sending it a String with the
* file name of the dictionary you want to use.
*/
public Dictionary (String wordsFileString) throws Exception {
// We'll use a HashSet to store the dictionary.
words = new HashSet<String>();
wordsExistThatStartWith = new HashSet<String>(); if (wordsFileString == null) {
throw new Exception("Can't open a null words file.");
} File wordsFile = new File(wordsFileString); // Make sure we can read the file.
if (wordsFile.canRead() == false) {
throw new Exception("Dictionary can't reads words from this file, make sure it exists and is readable: "+wordsFile.getAbsoluteFile());
} try {
// Open a buffer to the file.
BufferedReader br = new BufferedReader(new FileReader(wordsFile));
System.out.println("Opened file \""+wordsFile+"\", reading in dictionary..."); while (br.ready()) { String line = br.readLine().trim(); // Blank line
if (line.length() < 1) {
continue;
} // Add each word in the file to our dictionary.
words.add(line.toUpperCase()); // For each "prefix" in the word, remember that there is at least
// one word that starts with that prefix
for (int i=1; i < line.length(); i++) {
wordsExistThatStartWith.add(line.substring(0, i).toUpperCase());
} } } catch (IOException e) {
throw new Exception("An IO exception occurred when reading "+wordsFile+":\n"+e);
} // Make sure we read at least one word.
if (words.size() < 1) {
throw new Exception("No words found in file: "+wordsFile);
} System.out.println("Dictionary set up with "+words.size()+" words."); } /**
* Returns whether a word is in the dictionary.
*/
public boolean containsWord (String word) {
return words.contains(word.toUpperCase());
} /**
* Returns whether we found any words in the dictionary that started with some initial prefix.
*/
public boolean wordsExistThatStartWith (String prefix) {
return wordsExistThatStartWith.contains(prefix.toUpperCase());
}
}
private HashSet<String> words;
^
.\Dictionary.java:19: <identifier> expected
private HashSet<String> wordsExistThatStartWith;
^
.\Dictionary.java:30: '(' or '[' expected
words = new HashSet<String>();
^
我看了一下,好像Java SDK中本来就有个Dictionary类,是不是跟那个冲突了?下面是代码
import java.io.*;
import java.util.HashSet;public class Dictionary { private HashSet<String> words;
private HashSet<String> wordsExistThatStartWith; private File configurationFile; /**
* Construct the class by sending it a String with the
* file name of the dictionary you want to use.
*/
public Dictionary (String wordsFileString) throws Exception {
// We'll use a HashSet to store the dictionary.
words = new HashSet<String>();
wordsExistThatStartWith = new HashSet<String>(); if (wordsFileString == null) {
throw new Exception("Can't open a null words file.");
} File wordsFile = new File(wordsFileString); // Make sure we can read the file.
if (wordsFile.canRead() == false) {
throw new Exception("Dictionary can't reads words from this file, make sure it exists and is readable: "+wordsFile.getAbsoluteFile());
} try {
// Open a buffer to the file.
BufferedReader br = new BufferedReader(new FileReader(wordsFile));
System.out.println("Opened file \""+wordsFile+"\", reading in dictionary..."); while (br.ready()) { String line = br.readLine().trim(); // Blank line
if (line.length() < 1) {
continue;
} // Add each word in the file to our dictionary.
words.add(line.toUpperCase()); // For each "prefix" in the word, remember that there is at least
// one word that starts with that prefix
for (int i=1; i < line.length(); i++) {
wordsExistThatStartWith.add(line.substring(0, i).toUpperCase());
} } } catch (IOException e) {
throw new Exception("An IO exception occurred when reading "+wordsFile+":\n"+e);
} // Make sure we read at least one word.
if (words.size() < 1) {
throw new Exception("No words found in file: "+wordsFile);
} System.out.println("Dictionary set up with "+words.size()+" words."); } /**
* Returns whether a word is in the dictionary.
*/
public boolean containsWord (String word) {
return words.contains(word.toUpperCase());
} /**
* Returns whether we found any words in the dictionary that started with some initial prefix.
*/
public boolean wordsExistThatStartWith (String prefix) {
return wordsExistThatStartWith.contains(prefix.toUpperCase());
}
}
java -version
看下版本,要5.0以上才行
另外检查classpath
我在ide上看了,源码没问题
Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)classpath,已经包含了当前目录。CLASSPATH=.;F:\j2sdk1.4.2_16\lib
怎么classpath里面是1.4的呢
java version "1.4.2_16"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_16-b05)
Java HotSpot(TM) Client VM (build 1.4.2_16-b05, mixed mode)
public LinkedList<BoardLocation>
BoardLocation也是我做的另外一个类。
上面说的也是这样:
private HashSet<String> words;
private HashSet<String> wordsExistThatStartWith;
一加上Dictionary dict;就编译出错
不加就没问题
import java.io.*;public class BoggleSolver{
public static void main (String args[]) {
// BoardLocation loc;
// BoggleBoard board;
Dictionary dict;// int tmp;
// int len;
/* The user should take two arguments on the command-line */
if(args.length != 2) {
System.out.println("Error: Number of arguments should be two.");
System.exit(1);
}
/*
try {
board = new BoggleBoard(args[1]);
}
catch(Exception e) {
System.out.println(e);
System.exit(1);
}
System.out.println (args[0] + "\r\n" + args[1]);
loc = new BoardLocation(1, 2);
System.out.println(loc.row());
*/
}
}
6.0 所在目录 CLASSPATH
.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;PATH
%JAVA_HOME%\bin;