Just when I think I’ve got the basics of Java down (it’s been a full semester!), something happens to make me question that. I’m doing some practice review and have a bit of a problem. Here’s the code:
public class LetterCount
{
private char[] wordArray;
private int numVowels = 0, numConsonants = 0, numSpaces = 0, numDigits = 0;
public LetterCount(String str)
{
wordArray = str.toCharArray();
}
public int getNumVowels()
{
for (int count = 0; count < wordArray.length; count++)
{
if (wordArray[count] == 'a' || wordArray[count] == 'e' ||
wordArray[count] == 'i' || wordArray[count] == 'o' || wordArray[count]
== 'u' || wordArray[count] == 'y')
numVowels++;
}
return numVowels;
}
public int getNumDigits()
{
for (int count = 0; count < wordArray.length; count++)
{
if (Character.isDigit(wordArray[count]))
numDigits++;
}
return numDigits;
}
public int getWhiteSpace()
{
for (int count = 0; count < wordArray.length; count++)
{
if (Character.isSpaceChar(wordArray[count]))
numSpaces++;
}
return numSpaces;
}
public int getNumConsonants()
{
numConsonants = wordArray.length - getNumVowels() - getNumDigits() - getWhiteSpace();
return numConsonants;
}
public String toString()
{
String str = "Characters: " + wordArray.length + "\n" +
"Vowels: " + getNumVowels() + "\n" +
"Consonants: " + getNumConsonants() + "\n" +
"Digits: " + getNumDigits() + "\n" +
"Spaces: " + getWhiteSpace();
return str;
}
}
Here is the output:
Enter a sentence: this is a test 4 u
Characters: 18
Vowels: 5
Consonants: 2
Digits: 2
Spaces: 10
My questions:
1) I was sure I could use field names in the toString() method (e.g., numVowels vs. getNumConsonants()), but it seems this class requires me to use method names. When I use field names I get 0. Why the difference? I do know that if I return an equation, I have to use the method name.
2) I also don’t understand why my numConsonants() method doesn’t return the correct numbers. If I return each field separately (and call the method since I can’t call the field name), I get the correct number. Put them in an equation and it’s incorrect. What am I doing wrong?
Here’s the original main method. I’ve since edited it to call the methods in the LetterCount class:
import java.util.Scanner;
public class LetterCountDemo {
public static void main(String[] args)
{
String sentence;
LetterCount lc;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a sentence: ");
sentence = keyboard.nextLine();
lc = new LetterCount(sentence);
System.out.println(lc);
}
}
Added:
lc.getNumVowels();
lc.getNumConsonants();
Reason you have to use method instead of field is simple. Your methods are not simple accessor methods.(Check this link) They do some calculations and update the filed variables and return them. So until you call the method your fields are having value zero.
This way of updating field variable is bad. If you call the method twice (eg:
getNumVowels), it will update the already updated field variablenumVowels.Problem with
getNumConsonantsis related to above answer.Solution here would be not to use field variables and use local variable. See below example.
With this modification
getNumDigits()will always return correct answer.