Code first questions later…
public class Program2
{
//The custom word object used when parsing the input file
class Word
{
public String wordname;
public int count;
public int uniqueWord = 0;
public Word(String word)
{
wordname = word;
count = 1;
}
public boolean wordExists(String word)
{
if (word == this.wordname)
{
this.count++;
return true;
}
else
{
return false;
}
}
public int getCount(Word word)
{
return this.count;
}
public String getName(Word word)
{
return this.wordname;
}
}
// The main method
public static void main(String[] args) throws IOException
{
//new array of words size 100
Word[] words = new Word[100];
//set the first word to bananna
Word words[0] = new Word("bananna");
//print bananna
System.out.print(getName(words[0]));
}
}
Ok, so with what I know about Java, the code above should let me make an array of words, set the first to “bananna”, and print it out. I have little experience making a custom class like this, and I can’t find a good resource to model. Also, I am not 100% sure I understand calling static/nonstatic methods, so I’m sure some of the errors are from that as well.
What the program should do eventually, as a reference for why I am doing this, I have to take information in from a file (delimited strings aka Words) and see if it already exists in the array of words (and increment that word’s count if it does), if it doesn’t then make a new word.
Errors I’m getting are here:
Program2.java:116: ']' expected
Word words[0] = new Word("bananna");
^
Program2.java:116: illegal start of expression
Word words[0] = new Word("bananna");
^
2 errors
Any other information that you need let me know. I’ll be back to check this post in an hour. Thank you for any help you have!
Your main problem I can see is that
Word[100]is an Array type that holds Word objects. You’ll need the words variable to be of typeWord[], not just of typeWord.Regarding static and non-static, think of it this way: You’ve written a Word class, and then you can create as many Word objects as you like that belong to that class. When something is static, it means it belongs to the Word class, so it belongs to the definition of a word without belonging to any particular word. In contrast, something that is not static will belong to a particular Word object.