Results 1 to 3 of 3
  1. #1
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Using an array of objects

    I am given this class, WordCount.

    Java Code:
    public class WordCount{
    
      private String word;
      private int count;
    
      public WordCount(String w){
        word = w;
        count = 0;
      }
    
      public String getWord(){
        return word;}
      
       public int getCount(){
        return count;}
    
      public void incCount(){count++;}
    
      public String toString() {
        return(word +  " --- " + count);
      }
      
      public boolean equals(Object other){
        WordCount i = (WordCount)other;
        return (this.word.equals(i.word));
      }
    }
    I need to make another class, where it uses WordCount as an array of objects.In this class it takes a string of text, s, splits it into individuals words, then compares each word to another string w. If they match, it raises the count and in the final output it shows the word and number of counts.

    Java Code:
    public class ArrayPlay{
      public static void main (String[] args){
        
        WordCount[] objects=new WordCount[4];
        String s="hi how hi you hi";//the text
        String input="hi"; //the string to match to
        String[] words=s.split("\\s");
    
        for(String w: words){ //for every word, w,  in the words Array
          if(w.equals(input)){ // if the word, w, equals the user input
          for(int index=0;index<words.length;index++){
          System.out.println(objects[index].getWord()+ " "+ objects[index].getCount());//prints out the word and count at index i
          }
          
      }
    }
      }
    }
    Ok, I didnt worry about changing the count at this point...I'm just trying to get it to print out hi 0 right now, and while it compiles I get a
    NullPointerException. I'm guessing this has to do with WordCount objects=new WordCount[4]...Ideally the WordCount has sized based on the string length, so if I changed the string length it would still work...Can anyone point me in the right direction?
    Last edited by katiebear128; 11-17-2011 at 05:22 PM.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Using an array of objects

    Please post the whole NPE error message in the future.
    objects[index].getWord() <-- you never create WordCount objects and set it to index so objects[0] or objects[index] are null !
    objects[0] = new WordCount()..... or similar is missing

  3. #3
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Using an array of objects

    thank you! I fixed that problem.
    Java Code:
    public class ArrayPlay{
      public static void main (String[] args){
         String s="hi how hi you hi";//the text
        WordCount[] objects=new WordCount[4];
        String input="hi"; //the user input
        String[] words=s.split("\\s");
    
        for(String w: words){ //for every word, w,  in the words Array
          if(w.equals(input)){ // if the word, w, equals the user input
          for(int index=0;index<words.length;index++){
            objects[index]=new WordCount(w);
            objects[index].incCount();
          System.out.println(objects[index].getWord()+ " "+ objects[index].getCount());//prints out the word and count at index 
          }
          
      }
    }
    
        
      }
    }
    How come the count does not increase? Is it because of where my print statement is? I think I have to get rid of the print statement anyway and maybe use the toString() method... I'm not sure exactly how to do it, objects.toString() doesnt work...
    Last edited by katiebear128; 11-17-2011 at 06:25 PM.

Similar Threads

  1. Array of objects
    By EnSlavingBlair in forum New To Java
    Replies: 4
    Last Post: 09-30-2011, 01:27 PM
  2. How to convert array of Objects into array of Strings
    By elenora in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2011, 03:48 PM
  3. Array of objects
    By rosh72851 in forum New To Java
    Replies: 5
    Last Post: 10-31-2008, 04:03 AM
  4. Array of Objects
    By bluefloyd8 in forum New To Java
    Replies: 5
    Last Post: 01-22-2008, 06:27 PM
  5. Array with objects
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •