This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Counting vowels  RSS feed

 
Qriz Cruega
Greenhorn
Posts: 11
Chrome Eclipse IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,

I'm pretty new to Java and I am struggling to get this program to do what I want.
I browsed through many post and got decent progress but now I'm stuck.
Here is my code so far:


My problem is the count variable. Unlike the sum variable, I need the vowel count for each name and not the total.
Can someone please drop a hint on how I can achieve that???
It'll be much appreciated.
Thanks in advance.

Qriz
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Count needs to equal 0 every time you start counting the vowels in a name. So you need to initialize it inside your loop.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Qriz Cruega wrote:Can someone please drop a hint on how I can achieve that???

Yes. STOP CODING.

Your code has the look of someone who started coding with a partial idea of how to solve the problem, and is now trying to complete the task by brute force (ie, by coding).
IT WON'T WORK (and even if you somehow manage it in this case, it definitely won't work when you get on to more difficult problems).

Until you can describe the solution in English (or your native language), you will NEVER be able to code it in Java. So write it down (preferably, with you computer turned OFF).

Programming is thinking, not coding.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to Winston's excellent advice, when you do start coding, write the code for one small step at a time. Compile, test, and debug that code completely independently of any other code. Once it works, move on and write code for the next step, completely independently of any other code. Once that works, combine the code for those two steps, test, debug, and move on to the next step.

What you don't want to do is write down all your steps in English, then go and write a complete Java program for the whole thing, and only then try to test and debug it. That will lead to no end of misery.
 
Qriz Cruega
Greenhorn
Posts: 11
Chrome Eclipse IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Qriz Cruega wrote:Can someone please drop a hint on how I can achieve that???

Yes. STOP CODING.

Your code has the look of someone who started coding with a partial idea of how to solve the problem, and is now trying to complete the task by brute force (ie, by coding).
IT WON'T WORK (and even if you somehow manage it in this case, it definitely won't work when you get on to more difficult problems).

Until you can describe the solution in English (or your native language), you will NEVER be able to code it in Java. So write it down (preferably, with you computer turned OFF).

Programming is thinking, not coding.

Winston


Winston,

it took me a moment to not take this the wrong way, but I think I got the message now.

The assignment requires to have an array of 5 strings, to count the vowels and to display the total vowels.

So in simple words i will need to:

create the class
create a string array and populate it
create 2 integer variables (sum, length)
sum will hold the total, and length the size of the array
create a char variable to compare the strings whether or not they're vowels
loop through the array and display the names
nest another loop inside the first one to find the vowels via if statement and update the sum variable
display the total.

my revision of the code is here:


Output is:

Albert Barrack Christoph Denzel Elizabeth

Albert's vowels make it 2 total.
Barrack's vowels make it 4 total.
Christoph's vowels make it 6 total.
Denzel's vowels make it 8 total.
Elizabeth's vowels make it 12 total.

Even though I didn't get to display the vowel count for each name.
The Assignment is complete now
Thanks for the help

 
Qriz Cruega
Greenhorn
Posts: 11
Chrome Eclipse IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Deems wrote:Count needs to equal 0 every time you start counting the vowels in a name. So you need to initialize it inside your loop.


Thanks for your reply.
 
Qriz Cruega
Greenhorn
Posts: 11
Chrome Eclipse IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:In addition to Winston's excellent advice, when you do start coding, write the code for one small step at a time. Compile, test, and debug that code completely independently of any other code. Once it works, move on and write code for the next step, completely independently of any other code. Once that works, combine the code for those two steps, test, debug, and move on to the next step.

What you don't want to do is write down all your steps in English, then go and write a complete Java program for the whole thing, and only then try to test and debug it. That will lead to no end of misery.


Thank you as well, I will keep that in mind.
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Qriz Cruega wrote: . . .
So in simple words i will need to:

create the class
create a string array and populate it
create 2 integer variables (sum, length)
sum will hold the total, and length the size of the array
create a char variable to compare the strings whether or not they're vowels
loop through the array and display the names
nest another loop inside the first one to find the vowels via if statement and update the sum variable
display the total. . . .

Welcome to the Ranch

I am afraid that is not how Winston wanted you to think. What he expected was more like this
  • 1: Write down the count so far (0)
  • 2: Write the list of letters you are looking for (AEIOU)
  • 3: Find the first word.
  • 4: Find the first letter of the word.
  • 5: Find whether that letter is in your list: if so, increase the count by 1.
  • 6: If you have more letters, move on to the next letter, otherwise omit the following repeat.
  • 7: Repeat from step 5.
  • 8: Display count, and reset it to 0
  • 9: If you have any more words, go to the next word, otherwise omit following repeat..
  • 10: Repeat from step 4.
  • 11: Finished
  • What Winston wanted was how you would do it, which you can then try to convert to how the computer should do it. What you wrote is how you expected the computer to do it. You need to know what you would do yourself before you try to tell the computer to do anything. Those of us who have been doing this for a long time go straight to code, but it is not something you can learn to do quickly. And for difficult things, we still write such instructions (pseudo‑code), or diagrams of the data structure.
     
    Qriz Cruega
    Greenhorn
    Posts: 11
    Chrome Eclipse IDE PHP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Welcome to the Ranch

    I am afraid that is not how Winston wanted you to think. What he expected was more like this
  • 1: Write down the count so far (0)
  • 2: Write the list of letters you are looking for (AEIOU)
  • 3: Find the first word.
  • 4: Find the first letter of the word.
  • 5: Find whether that letter is in your list: if so, increase the count by 1.
  • 6: If you have more letters, move on to the next letter, otherwise omit the following repeat.
  • 7: Repeat from step 5.
  • 8: Display count, and reset it to 0
  • 9: If you have any more words, go to the next word, otherwise omit following repeat..
  • 10: Repeat from step 4.
  • 11: Finished
  • What Winston wanted was how you would do it, which you can then try to convert to how the computer should do it. What you wrote is how you expected the computer to do it. You need to know what you would do yourself before you try to tell the computer to do anything. Those of us who have been doing this for a long time go straight to code, but it is not something you can learn to do quickly. And for difficult things, we still write such instructions (pseudo‑code), or diagrams of the data structure.


    Thank you so much, that helps so much more then just code corrections.
    I really appreciate that you guys take the time to help beginners to understand.
     
    Campbell Ritchie
    Marshal
    Posts: 55678
    161
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also on behalf of Jeff Winston and Dennis …

    “You’re welcome
     
    Winston Gutkowski
    Bartender
    Posts: 10573
    65
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Qriz Cruega wrote:it took me a moment to not take this the wrong way, but I think I got the message now.

    The assignment requires to have an array of 5 strings, to count the vowels and to display the total vowels.
    So in simple words i will need to:
    create the class
    create a string array and populate it
    create 2 integer variables (sum, length)...

    Now you're getting the idea. And don't worry if you don't get the description absolutely correct the first time. Like all techniques, it takes practise; and to be honest, it's the process that's important. Basically, you've got to force yourself to think before you code.

    You might get some tips on technique here, but the best piece of advice I can give you when putting together your description is to try as hard as you can NOT to think in Java-ese. The first thing you should concern yourself with is WHAT needs to be done, not HOW you're going to do it. You'll have plenty of time to decide those nuts and bolts later.

    And sorry if I sounded snippy, but I was simply trying to stop you from thrashing around aimlessly.

    Winston
     
    Angus Comber
    Ranch Hand
    Posts: 90
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also encapsulation is your friend. Create an isVowel function. Eg
     
    Qriz Cruega
    Greenhorn
    Posts: 11
    Chrome Eclipse IDE PHP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    And sorry if I sounded snippy, but I was simply trying to stop you from thrashing around aimlessly.

    Winston


    Mission accomplished

    I have a totally different approach now.
    Thanks again.
     
    Qriz Cruega
    Greenhorn
    Posts: 11
    Chrome Eclipse IDE PHP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Angus Comber wrote:Also encapsulation is your friend. Create an isVowel function. Eg


    Thanks Angus
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!