我已经被这个问题困扰了几天,找不到这个问题的任何答案。它慢慢让我发疯。我需要制作两种方法,一种是将该单词添加到 Array Word[] 单词中,另一种是在单词出现多次时将其删除。可悲的是,每次我尝试使用 .add 或任何类似的东西都不起作用。我感到很困惑。
WordBagClient
public class WordBagClient {
public static void main(String[] args) throws IOException {
System.out.println("PMJ's WordBagClient ...");
String filename;
if(args.length > 0) { // Use run-time first argument value
filename = args[0];
}
else { // Prompt user to enter name of data file and store response
System.out.print("Enter name of input file:");
Scanner keyboard = new Scanner(System.in);
filename = keyboard.nextLine();
}
// Establish Scanner object that can read from the input data file.
Scanner input = new Scanner(new File(filename));
WordBag wordBag = new WordBag(256);
String s;
// Add each of the words until a blank line is encountered
do {
s = input.nextLine();
System.out.println(s);
if(s.length() > 0) {
addTask(s,wordBag);
}
} while (s.length() > 0);
printWordBag(wordBag);
// Remove an instance of each of the words until a second blank line is encountered
do {
s = input.nextLine();
System.out.println(s);
if(s.length() > 0) {
//removeTask(s,wordBag);
}
} while (s.length() > 0);
printWordBag(wordBag);
}
static void addTask(String s, WordBag wordBag) {
Scanner wordScanner = new Scanner(s);
while(wordScanner.hasNext()) {
String string = wordScanner.next();
String word = Word.wordOf(string);
wordBag.add(new Word(word));
}
}
static void removeTask(String s, WordBag wordBag) {
Scanner wordScanner = new Scanner(s);
while(wordScanner.hasNext()) {
String string = wordScanner.next();
String word = Word.wordOf(string);
Word object = new Word(word);
while(wordBag.multiplicityOf(object) > 0) {
wordBag.remove(object);
}
}
}
static void printWordBag(WordBag wordBag) {
System.out.println("The word bag now contains:");
System.out.println("--------------------------");
System.out.println(wordBag.toString());
System.out.println("--------------------------");
/*
wordBag.reset();
while(wordBag.hasNext()) {
System.out.print(wordBag.next());
if(wordBag.hasNext()) { System.out.print(","); }
}
System.out.println("\n"+"--------------------------");
*/
}
}
这是我的 WordBag.java
public class WordBag {
public static final int DEFAULT_CAPACITY = 8; //Default number of distince words
//allowed in a bag.
private int capacity; //The capacity of this instance's private arrays
private Word[] words; //The array to hold the words
private int[] counts; //The array to hold the corresponding counts
private int nextIndex; //Indicates the next available element position
public WordBag() {
this.words = new Word[DEFAULT_CAPACITY];
this.counts = new int[DEFAULT_CAPACITY];
}
public WordBag(int specifiedCapacity) {
if (capacity>-1){
this.capacity = specifiedCapacity;
this.words = new Word[capacity];
this.counts = new int[capacity];
}else{
this.capacity = DEFAULT_CAPACITY;
this.words = new Word[DEFAULT_CAPACITY];
this.counts = new int[DEFAULT_CAPACITY];
}
nextIndex = 0;
}
public boolean has(Word word) {
boolean dog = false;
do{
if(words[nextIndex].equals(word)){
return dog = true;
}else{
nextIndex++;
}
}while(nextIndex<=capacity && words[nextIndex]!=word);
return dog;
}
public int multiplicityOf(Word word) { //Stub!!!
int result=0;
do{
words[nextIndex].equals(word);
result = result+1;
}while(nextIndex<=capacity && words[nextIndex]!=word);
return result;
}
public void add(Word word) { //Stub!!!
do{
words[nextIndex]=(word);
}while(nextIndex<=capacity && words[nextIndex]!=word);
nextIndex++;
}
public void remove(Word word) { //Stub!!!
do{
if(multiplicityOf(word)>0){
word=null;
}else{
word=word;
}
}
while(nextIndex<=capacity && words[nextIndex]!=word);
}
private final String COMMA = ",";
public String toString() { //Stub!!!
String result = "";
result = Arrays.toString(words);
return result;
}
}
这是我的单词课
public class Word {
public static final String SYMBOLS = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?";
private String word = "";
public Word(String string) {
String word = Word.wordOf(string);
if(Word.isWord(word)) {
this.word = word;
}
}
public String toString() {
return word;
}
public boolean equals(Word that) {
return this.compareTo(that) == 0;
}
public int compareTo(Word that) {
return this.toString().compareTo(that.toString());
}
public static String wordOf(String s) {
String result = "";
//Trim of leading and trailing whitespace
s = s.trim();
//Scan over leading symbols
int start;
for(start=0; (start<s.length()) && (SYMBOLS.indexOf(s.charAt(start))>=0); start++) {
}
//Scan over trailing symbols
int stop;
for(stop=s.length()-1; (stop > start) && (SYMBOLS.indexOf(s.charAt(stop))>=0); stop--) {
}
//Isolate what is left in the middle
if(start <= stop) {
result = s.substring(start,(stop+1));
}
//Return
return result;
}
public static boolean isWord(String s) {
s = s.trim();
return (s.length() > 0) && (s.equals(Word.wordOf(s)));
}
}