Reputation: 83
I am trying to create a Word Class that processes every word that the file imports.
The Word Class needs to distinguish punctuation for the ending/beginning of sentences, increment valid words, and increment each syllable.
My problem is getting the scanner to pass the word onto the Word class for the methods to process them. the error is on "word = scan.next();." Error message is "incompatible types:Required Word, Found String."
Thank you for your help...
System.out.println("You chose to open the file: " +
fc.getSelectedFile().getName());
scan = new Scanner(fc.getSelectedFile());
while (scan.hasNext())
{
Word word = new Word();
word = scan.next();
Word Class
public class Word {
private int wordCount, syllableCount, sentenceCount;
private double index;
private char syllables [] = {'a', 'e', 'i', 'o', 'u', 'y'};;
private char punctuation [] = {'!', '?','.'};;
public Word()
{
wordCount = 0;
syllableCount = 0;
sentenceCount = 0;
}
public void Word(String word)
{
if(word.length() > 1)
{
for(int i=0; i < punctuation.length; i++)
{
if(punctuation[i] != word.charAt(word.length()-1))
{
wordCount++;
}
else
sentenceCount++;
}
for(int i = 0; i < syllables.length; i++)
{
for(int j = 0; j < word.length(); j++)
{
if(syllables[i] == word.charAt(j))
{
syllableCount++;
}
}
}
}
System.out.println(word);
}
}
Upvotes: 0
Views: 1534
Reputation:
The problem is that scan.next()
returns a String
not a Word
object you can not assign it like that.
Try this:
while (scan.hasNext()) {
Word word = new Word(scan.next());
//...
}
To do this, you need a constructor like that:
public Word(String s){
//...
}
Upvotes: 2