Results 21 to 29 of 29
Thread: Vowels
-
Re: Vowels
I'm sorry -- you do, and the numbers in your output seem to add up correctly.
Don't give up as you seem to be getting the hang of this.
- 12-02-2012, 12:29 AM #22
Member
- Join Date
- Nov 2012
- Posts
- 44
- Rep Power
- 0
Re: Vowels
to be honest...i'm really not getting this. Im using the internet for ideas and then i take some of those ideas and mix them together. If it weren't for the internet....i would be screwed....
- 12-02-2012, 12:33 AM #23
Member
- Join Date
- Nov 2012
- Posts
- 44
- Rep Power
- 0
Re: Vowels
ok....so now that everything adds up correctly...how on earth do I ask the user if they want to do another string of characters using the while loop???
- 12-02-2012, 01:27 AM #24
Member
- Join Date
- Nov 2012
- Posts
- 44
- Rep Power
- 0
Re: Vowels
ok i created this palindrome tester from my book that uses a while loop and asks the user if they want to do another one.
But how would I use that with my my vowels program?Java Code:while (left < right) { charLeft = str.charAt(left); charRight = str.charAt(right); if (charLeft == charRight) { left++; right--; } else if (charLeft == ',' || charLeft == '.' || charLeft == '-' || charLeft == ':' || charLeft == ';' || charLeft == ' ') left++; else if (charRight == ',' || charRight == '.' || charRight == '-' || charRight == ':' || charRight == ';' || charRight == ' ') right--; else break; } System.out.println(); if (left < right) System.out.println ("That string is NOT a palindrome."); else System.out.println ("That string IS a palindrome."); System.out.println(); System.out.print ("Test another palindrome (y/n)? "); another = scan.nextLine(); } }
- 12-02-2012, 02:06 AM #25
Member
- Join Date
- Nov 2012
- Posts
- 44
- Rep Power
- 0
Re: Vowels
Alright I think I got it now.....here it is
[CODE]
package vowels;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
String strCount, another = "y";
int aCount=0, eCount=0, iCount=0, oCount=0, uCount=0, count = 0;
Scanner scan = new Scanner (System.in);
while (another.equalsIgnoreCase("y"))
{
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':
case 'A':
aCount++;
break;
case 'e':
case 'E':
eCount++;
break;
case 'i':
case 'I':
iCount++;
break;
case 'o':
case 'O':
oCount++;
break;
case 'u':
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 ("Other characters = " + (strCount.length() - (aCount + eCount + iCount + oCount + uCount)));
System.out.println ();
System.out.print ("Would you like to enter another string (y/n)? ");
another = scan.nextLine();
}
}
}
And here is the output.....
Please Enter a string of characters:
Hello there
a: 0
e: 3
i: 0
o: 1
u: 0
Other characters = 7
Would you like to enter another string (y/n)? y
Please Enter a string of characters:
My name is ryan
a: 2
e: 4
i: 1
o: 1
u: 0
Other characters = 7
Would you like to enter another string (y/n)? n
BUILD SUCCESSFUL (total time: 1 minute 25 seconds)
- 12-02-2012, 02:10 AM #26
Member
- Join Date
- Nov 2012
- Posts
- 44
- Rep Power
- 0
Re: Vowels
Nevermind i didn't get it...ugh...................now the prob is that it is counting all the vowels and other characters from the previous string.........ugh........what do I need to do now!!?? x(
-
Re: Vowels
You need to remember that all that needs to be looped, needs to be inside of the loop. So either declare all of those variables inside of the loop at the beginning of the loop so that they are created anew each time the loop iterates, or create them before the loop as you are doing, but initialize them, set them to default values inside of the loop at the start of the loop.
You are making good progress, trust me. Keep this up and you will be one of the smart ones in your class.
- 12-02-2012, 05:32 PM #28
Member
- Join Date
- Nov 2012
- Posts
- 44
- Rep Power
- 0
Re: Vowels
I thought I did intitalize them all???
-
Re: Vowels
You do -- but the key is where do you initialize them? If you initialize them before the while loop, then they won't be reset with each iteration of the loop. If on the other hand you initialize them inside of the while loop, then the variables will be reset. So look at your code above (you may wish to fix the code tags too), and see where you initialize:
Java Code:// do you initialize your variables here, before the while loop? while (somethingIsTrue) { // or do you initialize them here inside and at the start of the while loop? // ..... the rest of the while loop code has been deleted for sake of brevity }
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