1

I've been stuck on this problem for a few days and can't find any answers to this question. It's slowly driving me insane. I need to make a two methods, one that add that word to the Array Word[] words and one that removes it if the word occurs more than once. Sadly, every time I try to use .add or anything of the sort it doesn't work. I am so confused.

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"+"--------------------------");
  */
 }
 }

This is my 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;
  }





}

This is my Word Class

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)));   
}
}
Alex
  • 11
  • 2
  • 1
    Can you explain where exactly your problem is? In which line does it occur? – Grunzwanzling Nov 24 '16 at 09:14
  • Please have a look at your logic. I suspect the indices are not being maintained correctly (ans example case is remove() method). Why are you not using counts array to store your counts? The above in all seems to be an incomplete implementation(and a homework like question). I would suggest to use Map for storing word and their counts. – abksrv Nov 24 '16 at 09:16
  • My exact issue with this program is that I don't understand what I should be using to store the word to the array Word[] words. Every time I use .add or something similar, I get the response "cannot revolve symbol" or something akin tho that. – Alex Nov 24 '16 at 20:46
  • The same goes for the remove method, which was why I ended up being forced to try strange commands that don't make use of the arrays. – Alex Nov 24 '16 at 20:46

0 Answers0