Results 1 to 20 of 29
Thread: Vowels
- 11-30-2012, 11:58 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 44
- Rep Power
- 0
Vowels
Hello everyone...I am new.....I am having problems writing a and starting a project that will read a String from the keyboard, and count the number of each of the vowels. My program also needs to count other characters. There should be a separate variable as a counter for each. I need to use for loops and switch statements. Any ideas?
-
Re: Vowels
Let's start with your ideas -- what have you tried, and where exactly are you stuck? It would be best if you show us your current code, but have the posted code wrapped in code tags:
[code]
your posted code
[/code]
Also remember to break down any project into small steps and try to solve each small step one at a time so as not to get overwhelmed.
- 12-01-2012, 03:06 AM #3
Member
- Join Date
- Nov 2012
- Posts
- 44
- Rep Power
- 0
Re: Vowels
Alright here is what i got so far....
1st: I know this is not logically correct. I just started typing in random things to see what could fit together. I know the code is more complicated then this.Java Code:package vowels; import java.util.Scanner; public class Main { public static void main(String[] args) { int aCount=0, eCount=0, iCount=0, oCount=0, uCount=0, other=0; String strCount; Scanner scan = new Scanner(System.in); System.out.println("Please Enter a string of characters: "); aCount = scan.nextInt(); eCount = scan.nextInt(); iCount = scan.nextInt(); oCount = scan.nextInt(); uCount = scan.nextInt(); other = scan.nextInt(); switch (aCount) { case 1: strCount = "a: " + aCount; break; } switch (eCount) { case 2: strCount = "e: " + eCount; break; } switch (iCount) { case 3: strCount = "i: " + iCount; break; } switch (oCount) { case 4: strCount = "o: " + oCount; break; } switch (uCount) { case 5: strCount = "u: " + uCount; break; } } }
2nd: I don't want to type in a string of characters and then it to print out a e i o u and other characters separately. I want the computer to tell me how many vowels are in that string of characters and how many other characters as well. How do I do that?
-
Re: Vowels
Don't code random things as this never works. Instead think logically through the problem and then try to use that logic in your code. The logic could start with something like, if you were given a sentence on a piece of paper, how would you manually go through the sentence and count the number of vowels.
- 12-01-2012, 09:58 PM #5
Member
- Join Date
- Nov 2012
- Posts
- 44
- Rep Power
- 0
Re: Vowels
That's my problem, I have no idea where to start. I don't know what half these things mean and Ive read the chapter like 2 times....nothing helps.
-
Re: Vowels
You will not be able to solve this until you understand Java a little better. Consider trying to study from a different tutorial as sometimes you can learn better from one source vs another.
Please have a look here: The Really Big Index
- 12-01-2012, 11:00 PM #7
Member
- Join Date
- Nov 2012
- Posts
- 44
- Rep Power
- 0
Re: Vowels
Ok i have redone this program....here is what I got..
But when I run that program it prints out this.....Java Code:package vowels; import java.util.Scanner; public class Main { public static void main(String[] args) { int aCount=0, eCount=0, iCount=0, oCount=0, uCount=0; Scanner scan = new Scanner (System.in); String strCount; System.out.println("Please Enter a string of characters: "); strCount = scan.nextLine(); char vowels = 0; for (int count = 0; count < strCount.length(); count++) { vowels = strCount.charAt(count); switch (vowels) { case 'a': aCount++; break; case 'e': eCount++; break; case 'i': iCount++; break; case 'o': oCount++; break; case 'u': uCount++; break; } System.out.println ("a: " + aCount); System.out.println ("e: " + eCount); System.out.println ("i: " + iCount); System.out.println ("o: " + oCount); System.out.println ("u: " + uCount); System.out.println ("Constants = " + (strCount.length() - (aCount + eCount + iCount + oCount + uCount))); } } }
Please Enter a string of characters:
Hello there! My name is Ryan.
a: 0
e: 0
i: 0
o: 0
u: 0
Constants = 29
a: 0
e: 1
i: 0
o: 0
u: 0
Constants = 28
a: 0
e: 1
i: 0
o: 0
u: 0
Constants = 28
a: 0
e: 1
i: 0
o: 0
u: 0
Constants = 28
a: 0
e: 1
i: 0
o: 1
u: 0
Constants = 27
a: 0
e: 1
i: 0
o: 1
u: 0
Constants = 27
a: 0
e: 1
i: 0
o: 1
u: 0
Constants = 27
a: 0
e: 1
i: 0
o: 1
u: 0
Constants = 27
a: 0
e: 2
i: 0
o: 1
u: 0
Constants = 26
a: 0
e: 2
i: 0
o: 1
u: 0
Constants = 26
a: 0
e: 3
i: 0
o: 1
u: 0
Constants = 25
a: 0
e: 3
i: 0
o: 1
u: 0
Constants = 25
a: 0
e: 3
i: 0
o: 1
u: 0
Constants = 25
a: 0
e: 3
i: 0
o: 1
u: 0
Constants = 25
a: 0
e: 3
i: 0
o: 1
u: 0
Constants = 25
a: 0
e: 3
i: 0
o: 1
u: 0
Constants = 25
a: 0
e: 3
i: 0
o: 1
u: 0
Constants = 25
a: 1
e: 3
i: 0
o: 1
u: 0
Constants = 24
a: 1
e: 3
i: 0
o: 1
u: 0
Constants = 24
a: 1
e: 4
i: 0
o: 1
u: 0
Constants = 23
a: 1
e: 4
i: 0
o: 1
u: 0
Constants = 23
a: 1
e: 4
i: 1
o: 1
u: 0
Constants = 22
a: 1
e: 4
i: 1
o: 1
u: 0
Constants = 22
a: 1
e: 4
i: 1
o: 1
u: 0
Constants = 22
a: 1
e: 4
i: 1
o: 1
u: 0
Constants = 22
a: 1
e: 4
i: 1
o: 1
u: 0
Constants = 22
a: 2
e: 4
i: 1
o: 1
u: 0
Constants = 21
a: 2
e: 4
i: 1
o: 1
u: 0
Constants = 21
a: 2
e: 4
i: 1
o: 1
u: 0
Constants = 21
BUILD SUCCESSFUL (total time: 7 seconds)
What is wrong with my program?
-
Re: Vowels
Hey, that's starting to look pretty good!
The main thing that I see wrong is the location of where you're outputting your results. Consider outputting the results outside of the for loop, in other word after the closing curly brace of the for loop. Note that you have not indented your switch block inside of the for loop and so it is hard to see where one block ends. This is why it is so critical to be precise and consistent with your indentation and code formatting.
Good going so far!
- 12-01-2012, 11:19 PM #9
Member
- Join Date
- Nov 2012
- Posts
- 44
- Rep Power
- 0
Re: Vowels
Alright how about this.....
then it prints out this......Java Code:package vowels; import java.util.Scanner; public class Main { public static void main(String[] args) { int aCount=0, eCount=0, iCount=0, oCount=0, uCount=0, count = 0; Scanner scan = new Scanner (System.in); String strCount; System.out.println("Please Enter a string of characters: "); strCount = scan.nextLine(); char vowels = 0; for (count = 0; count < strCount.length(); count++) { vowels = strCount.charAt(count); switch (vowels) { case 'a': aCount++; break; case 'e': eCount++; break; case 'i': iCount++; break; case 'o': oCount++; break; case 'u': uCount++; break; } } System.out.println ("a: " + aCount); System.out.println ("e: " + eCount); System.out.println ("i: " + iCount); System.out.println ("o: " + oCount); System.out.println ("u: " + uCount); System.out.println ("Constants = " + (strCount.length() - (aCount + eCount + iCount + oCount + uCount))); } }
Please Enter a string of characters:
Hello there! My name is Ryan.
a: 2
e: 4
i: 1
o: 1
u: 0
Constants = 21
BUILD SUCCESSFUL (total time: 8 seconds)
-
Re: Vowels
You tell me.... how does the output look to you?
Regarding the code formatting -- it still needs work. Remember, any block nested inside of another block needs to be indented from the containing block. Again your switch block is not. In fact your whole formatting looks a bit off kilter. Look at some of the better code examples on this forum to see how to do this correctly.
- 12-01-2012, 11:30 PM #11
Member
- Join Date
- Nov 2012
- Posts
- 44
- Rep Power
- 0
Re: Vowels
I guess it looks ok...but I dont think its counting !, spaces, periods, and any other character. Plus it won't count capital letters. After I get that step, i need to put a while loop in to ask the user if they want to enter another string by using y and for the program to keep reading in strings and counting their vowels and other characters. This is so complicated................
- 12-01-2012, 11:48 PM #12
Senior Member
- Join Date
- Nov 2012
- Posts
- 228
- Rep Power
- 1
Re: Vowels
in regards to counting capital letters couldnt you tell it to ignore case so A is equal to an a;
so something like
sorry if im wrong, i based that off a program we created in school, it might not apply to yoursJava Code:String tennis=""; while(tennis.equalsIgnoreCase (" ")) { //then do switch stuff }
- 12-01-2012, 11:54 PM #13
Member
- Join Date
- Nov 2012
- Posts
- 44
- Rep Power
- 0
Re: Vowels
well the prob with that is that I still need that for loop in there. And my while loop will be for asking the user if they want to do another or not. I can try some of that though and ill see what results i get.
- 12-02-2012, 12:02 AM #14
Member
- Join Date
- Nov 2012
- Posts
- 44
- Rep Power
- 0
Re: Vowels
ya i have no idea where on earth I would put that...lol.....i tried putting it in but it completely messed up my program....im probably putting it in wrong somewhere
-
Re: Vowels
Do you want it to count these?
Two easy ways to solve this: 1) convert the entire String to lower-case before analyzing it. String has a method that allows you to do this, or 2) change your case statements to recognize both upper and lower case letters:Plus it won't count capital letters.
Java Code:case 'a': case 'A': // do something for a break; case 'b': case 'B': // do something for b break;
Solve one step at a time. Once you get the main program logic working, then wrap all that needs to loop in a while loop.After I get that step, i need to put a while loop in to ask the user if they want to enter another string by using y and for the program to keep reading in strings and counting their vowels and other characters. This is so complicated................
- 12-02-2012, 12:08 AM #16
Senior Member
- Join Date
- Nov 2012
- Posts
- 228
- Rep Power
- 1
Re: Vowels
im not even sure thats correct! my program might be completely different to yours, just trying to help a bit :)
you could still have the for loop, and the while loop you are aware of nesting loops arent you? *not to sound degrading
but as i say this might not be a correct answer, you could just use an or to say a OR A then count?
- 12-02-2012, 12:13 AM #17
Member
- Join Date
- Nov 2012
- Posts
- 44
- Rep Power
- 0
Re: Vowels
[QUOTE=Fubarable;312198]
Hey!!! That worked!!!!Java Code:case 'a': case 'A': // do something for a break; case 'b': case 'B': // do something for b break;
Yes I do want my program to count these.
Do you want it to count these?
-
Re: Vowels
- 12-02-2012, 12:19 AM #19
Member
- Join Date
- Nov 2012
- Posts
- 44
- Rep Power
- 0
Re: Vowels
hahaha yes i do but i hardly know anything about this....im in a class with smart kids who dont care about anyone but themselves....then there is kids who don't give a crap about this...and then there is me....taking notes like a madman....I want to learn..it's just so difficult. Plus when I take notes...the teacher writes on the board what he doesn't want to see. So im taking notes on the stuff he doesn't want. Like really?
- 12-02-2012, 12:21 AM #20
Member
- Join Date
- Nov 2012
- Posts
- 44
- Rep Power
- 0
Similar Threads
-
Counting Vowels, getting an error
By gabrielpr12 in forum New To JavaReplies: 8Last Post: 01-07-2012, 07:50 PM -
convert all vowels to the next alphabet and consonant to previous alphabet
By somnath6088 in forum New To JavaReplies: 2Last Post: 11-08-2010, 08:01 AM -
counting the vowels in a sentence
By huntrguy102 in forum New To JavaReplies: 3Last Post: 11-08-2010, 06:31 AM -
Replacing Vowels in a word.
By mklprasad in forum Advanced JavaReplies: 1Last Post: 10-05-2009, 12:31 PM -
Counting Vowels and Constonants
By MattN in forum New To JavaReplies: 3Last Post: 11-20-2007, 05:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks