Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8959831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:32:00+00:00 2026-06-15T15:32:00+00:00

Just when I think I’ve got the basics of Java down (it’s been a

  • 0

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:32:01+00:00Added an answer on June 15, 2026 at 3:32 pm

    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 variable numVowels.

    Problem with getNumConsonants is related to above answer.

    Solution here would be not to use field variables and use local variable. See below example.

    public int getNumDigits()
    {
        int numDigits = 0;
        for (int count = 0; count < wordArray.length; count++)
        {
            if (Character.isDigit(wordArray[count]))
                numDigits++;
        }
        return numDigits;
    }
    

    With this modification getNumDigits() will always return correct answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

just think that when I opened my file then when I want to write
I've been thinking about this and I just can't think of a way to
I think this is just a Best Practices question, but I was wondering if
I think I'm just missing something simple here but here is what I am
[self performSelector:@selector(stopPulling) withObject:nil afterDelay:0.01]; The code is fine. I just think that using NSOperation
I just think that it is convenient for me to cd to the directory
I was just think that now it is common to have enough RAM on
Has appeared a strange situation with listboxitem inner sender (i just think that this
I just think about that, easy to do in non cms site, but in
I have read much information about agile and waterfall and I just cannot think

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.