我正在进行java类分配。该计划应该有助于拼字游戏。它使用包含所有官方拼字游戏单词的源文件Dictionary.txt
,并根据用户的输入使用它生成输出文件output.txt
。当我尝试运行测试仪时,我得到了NoClassDefFoundError
。有人可以帮我弄清楚原因,请问?
WordLists类:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class WordLists
{
ArrayList<String> output;
File inFile;
String word;
Scanner input;
public WordLists(String fileName) throws FileNotFoundException
{
inFile = new File(fileName);
input = new Scanner(inFile);
output = new ArrayList<String>();
}
public ArrayList<String> lengthN(int n)
{
while(input.hasNextLine())
{
word = input.nextLine();
if (word.length() == n)
{
output.add(word);
word = input.nextLine();
}
}
return output;
}
public ArrayList<String> startsWith(int n, char firstLetter)
{
while(input.hasNextLine())
{
word = input.nextLine();
if ((word.length() == n) && (word.charAt(0) == firstLetter))
{
output.add(word);
word = input.nextLine();
}
}
return output;
}
public ArrayList<String> containsLetter(int n, char included)
{
while(input.hasNextLine())
{
word = input.nextLine();
if ((word.length() == n) && (word.contains(String.valueOf(included))))
{
output.add(word);
word = input.nextLine();
}
}
return output;
}
public ArrayList<String> vowelHeavy(int n, int m)
{
int vowels = 0;
while(input.hasNextLine())
{
word = input.nextLine();
if (word.length() == n)
{
for(int i = 0; i < word.length(); i++)
{
if(isVowel(word.charAt(i)) == true)
{
vowels++;
}
}
if (vowels == m)
{
output.add(word);
}
}
word = input.nextLine();
}
return output;
}
public ArrayList<String> multiLetter(int m, char included)
{
while(input.hasNextLine())
{
word = input.nextLine();
if (word.contains(String.valueOf(included)))
{
int occurences = 0;
for(int i = 0; i < word.length(); i++)
{
if( word.charAt(i) == included)
{
occurences++;
}
}
if (occurences == m)
{
output.add(word);
}
}
word = input.nextLine();
}
return output;
}
public boolean isVowel(char c)
{
if(c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O')
{
return true;
}
else
{
return false;
}
}
}
测试员类:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class Tester
{
public static void main(String[] args)throws FileNotFoundException
{
WordLists wl;
Scanner in;
int n;
int m;
char firstLetter;
char included;
try
{
wl = new WordLists("dictionary.txt");
}
catch (FileNotFoundException e)
{
System.out.println("file not found");
return;
}
in = new Scanner(System.in);
String outFile = "output.txt";
PrintWriter writer = new PrintWriter(outFile);
System.out.println("Search by length? Enter yes or no");
String response = in.next();
if(response.equals("yes"))
{
System.out.println("Enter word length");
n = in.nextInt();
wl.lengthN(n);
for(String a : wl.output)
{
writer.println(a);
}
writer.close();
}
else
{
System.out.println("Search by length and first letter?");
response = in.next();
}
if(response.equals("yes"))
{
System.out.println("Enter word length");
n = in.nextInt();
System.out.println("Enter the first letter");
firstLetter = in.nextLine().charAt(0);
wl.startsWith(n, firstLetter);
for(String a : wl.output)
{
writer.println(a);
}
writer.close();
}
else
{
System.out.println("Search by length and letter?");
response = in.next();
}
if (response.equals("yes"))
{
System.out.println("Enter word length");
n = in.nextInt();
System.out.println("Enter the letter");
included = in.nextLine().charAt(0);
wl.containsLetter(n, included);
for(String a : wl.output)
{
writer.println(a);
}
writer.close();
}
else
{
System.out.println("Search by number of vowels?");
response = in.next();
}
if(response.equals("yes"))
{
System.out.println("Enter word length");
n = in.nextInt();
System.out.println("Enter the number of vowels");
m = in.nextInt();
wl.vowelHeavy(n, m);
for(String a : wl.output)
{
writer.println(a);
}
writer.close();
}
else
{
System.out.println("Search by number of occurences of a letter?");
response = in.next();
}
if(response.equals("yes"))
{
System.out.println("Enter the number of occurences");
m = in.nextInt();
System.out.println("Enter the letter");
included = in.nextLine().charAt(0);
wl.multiLetter(m, included);
for(String a : wl.output)
{
writer.println(a);
}
writer.close();
}
}
}
这是我得到的错误:
Exception in thread "main" java.lang.NoClassDefFoundError: Tester/java
Caused by: java.lang.ClassNotFoundException: Tester.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: Tester.java. Program will exit.
答案 0 :(得分:0)
您似乎忘了编译代码,或者如果您使用的是IDE,则可能已将其配置为运行名为Tester.java
的类而不是Tester
。
从保存 WordLists.java 和 Tester.java 的目录中尝试以下操作:
javac WordLists.java Tester.java
java -cp . Tester