Results 1 to 3 of 3
Thread: Using an array of objects
- 11-17-2011, 05:17 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
Using an array of objects
I am given this class, WordCount.
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 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)); } }
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 aJava 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 } } } } }
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.
- 11-17-2011, 05:42 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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
- 11-17-2011, 06:05 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
Re: Using an array of objects
thank you! I fixed that problem.
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...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 } } } } }Last edited by katiebear128; 11-17-2011 at 06:25 PM.
Similar Threads
-
Array of objects
By EnSlavingBlair in forum New To JavaReplies: 4Last Post: 09-30-2011, 01:27 PM -
How to convert array of Objects into array of Strings
By elenora in forum Advanced JavaReplies: 1Last Post: 06-10-2011, 03:48 PM -
Array of objects
By rosh72851 in forum New To JavaReplies: 5Last Post: 10-31-2008, 04:03 AM -
Array of Objects
By bluefloyd8 in forum New To JavaReplies: 5Last Post: 01-22-2008, 06:27 PM -
Array with objects
By toby in forum New To JavaReplies: 1Last Post: 07-25-2007, 09:50 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks