-
Java for loops to count vowels/consonants usinf the logic of the main
When I enter 1 vowel the ans. is 1 vowel and 2 consonants. When I enter 1 vowel and 1 consonant the ans. is 1 v & 2 cons. When I enter 1 consonant the ans. is 0 v & 1 cons. If I enter the sentence: Welcome to Foothill. the ans. is 7 vowels & 8 consonants. The vowels are correct, but the consonants should be 10. I have tried several ways (for loop w/ if else & else and putting the control stat. within the braces, I have tried Strings & arrays but my knowledge is lacking, for ex. on the array used to count the chars my ans would give me a number value. I have reread my text book on loops & control statements several times however I must be missing the key concept to take care of my problem.
Code java:
import java.util.Scanner;
public class NewFoothill
{
public static void main(String [] args) throws Exception
{
Scanner input = new Scanner (System.in);
int count = 0;
System.out.println(" Enter the String. ");
String s1 = input.nextLine();
s1 = s1.toUpperCase();
System.out.println("======RESULT======" + s1);
s1 = s1.toLowerCase();
System.out.println("======RESULT======" + s1);
System.out.println(" String s1 ");
for (int i = 0; i < s1.length(); i++)
{
char c = s1.charAt(i);
if ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' )
{
count++;
}
}
System.out.println(" There are " + " " + count + " " + " vowels. ");
for (int i = 0; i < s1.length(); i++)
{
char c = s1.charAt(i);
if ( c != 'a' || c != 'e' || c != 'i' || c != 'o' || c != 'u' )
{
count++;
break;
}
}
System.out.println(" There are ' + " " + count + " " + " consonants. ");
}
}
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
One logic problem I see is this:
if ( c != 'a' || c != 'e' || c != 'i' || c != 'o' || c != 'u' )
If any one of those conditions is true (OR connected expression) then the whole condition will be true. If c is 'e' then it is not equal to 'a'
When do you want the condition to be true?
Another way to determine if a letter is in a group of letters would be to make a String of all the letters in the group and use the index of method to see if the selected letter is in the String.
Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
Norm thanks for your reply. I separated the if statements in each for loop. When I remove the loop that counts consonants my loop that counts vowels works fine and I get the same result when I remove the loop that counts vowels, my loop that counts consonants works fine. However, when I put them together there is a problem. For example when I enter a vowel that is counted correctly, but the loop jumps down and adds five consonants. I believe it counts the five vowels that I have asked the consonant loop to not count. How can I end this problem and get the two loops to work together and give me the correct output?
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
Post your current code.
Can you describe in detail in pseudo how the program should work to solve the problem? Write a list of the steps the program should do.
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
Thanks Norm for your reply. In each loop I separated each if statement. The loop that counts vowels works fine alone. I get the same good outcome when the loop that counts the consonants is alone without the other loop above it. If I enter 1 vowel, it is counted correctly, but the program goes down to the consonant loop and counts five consonants ( answer: There are 1 vowels. There are 5 consonants.). I do not understand why the for loop that counts consonants counts the five vowels that I asked that loop to not count. Since your last post to me, I have gone back to my textbooks, but I have not found an answer that works. I saw in some code that was used to get a password that boolean validated and not validated was used. Would something like that work? I am going crazy over this. It took me a very long time to ask for help and I realy need it now.
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
Did you miss my last post? Here it is again:
Post your current code.
Can you describe in detail in pseudo code how the program should work to solve the problem? Write a list of the steps the program should do.
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
Hi Norm, I got your reply and I did what you asked of me, but before I sent my new quick reply I went up and hit the thanks button before I sent my new reply with the code then I scrolled down to my new quick reply and it was gone. Did you get it? If not can I get my quick reply back so I can send it?
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
I guess it's gone. You'll have to try again.
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
Norm, it is 9:33PM here and my wife is out of town and my 5 year old wants me to feed him so I must sign off. I will try to contact you tomorrow. Wille Lee
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
no problems. Whenever you get to it.
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
Here is my current code Norm.
Code java:
import java.util.Scanner;
public class NewFoothill
{
public static void main(String[] args) throws Exception
{
Scanner input = new Scanner(System.in);
int count = 0;
System.out.println(" Enter the String ");
String s1 = input.nextLine();
s1 = s1.toUpperCase();
System.out.println("=====RESULT=====" + s1);
s1 = s1.toLowerCase();
System.out.println("=====RESULT=====" + s1);
System.out.println(" String s1 ");
for (int i = 0; i < s1.length(); i++)
{
char c = s1.charAt(i);
if ( c == 'a' )
{
count++;
}
if ( c == 'e' )
{
count++;
}
if ( c == 'i' )
{
count++;
}
if ( c == 'o' )
{
count++;
}
if ( c == 'u' )
{
count++;
continue;
}
}
System.out.println(" There are " + " " + count + " " + " vowels. ");
for ( int i = 0; i < s1.length(); i++)
{
char c = s1.charAt(i);
if ( c == 'c' )
{
count++;
continue;
}
if ( c == 'f' )
{
count++;
continue;
}
if ( c == 'w' )
{
count++;
continue;
}
if ( c == 'l' )
{
count++;
continue;
}
if ( c == 'm')
{
count++;
continue;
}
if ( c == 't' )
{
count++;
continue;
}
if ( c == 'h' )
{
count++;
continue;
}
if ( c != 'a' )
{
count++;
break;
}
if ( c != 'e' )
{
count++;
break;
}
if ( c != 'i' )
{
count++;
break;
}
if ( != 'o' )
{
count++;
break;
}
if ( c != 'u' )
{
count++;
break;
}
}
System.out.println(" There are " + " " + count + " " + " consonants. ");
}
}
The program should take the String input and send it to upper case & then to lower case. Next the 1st for loop following the String s1 should count all of the vowels in String s1. The following for loop should then count all of the consonants. I have tested each for loop independently & they work correctly. However, when the consonant loop follows the vowel loop the consonant loop does not work correctly. I took the sentence "Welcome to Foothill." to use as my test String & only entered the letters within that sentence as code in the program. In the 1st loop I stated that c equals each of the vowels separately. In the 2nd loop that counts the consonants I have c equaling the consonants that are in the above noted sentence & c not equaling the vowels so I wanted this loop to count only the consonants and not the vowels. However when I test the program I get these results: a = 1 vowel & 2 consonants. az = 1 v. & 2 c. z = 1 c. Welcome = 3 v. & 5c. to = 1 v. & 3 c. Foothill = 3 v. & 5 c. (which is correct) Welcome to Foothill. = 7 v. & 9 c. I have changed and reworked these for loops millions of times, but to no avail. I have reread my text books several times and I am still missing the point, that is why I turned to you for assistance. I have come against a brick wall.
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
Does your version of the code compile without errors? The posted code has errors.
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
Hi Norm, I posted all the information you asked for a 2nd time in the quick reply section and it looks like I posted my code again in the wrong place. This time I am trying the "Reply to Thread" section. This will be the 3rd attempt. If this does not work, please offer suggestions on how I can send the information and code to you.
My goal, besides getting the code to you, is to have two for loops enter the sentence in String s1 and pull out the vowels and count them 1st and then I want the program to go down to the next for loop and do the same with the consonants. In order to achieve this goal I have used if statements which will be either true or false. In the for loop dealing with the vowels I have made a separate if statement for each vowel. I then tested the loop separately and it worked perfectly. Next, I set up the for loop to count the consonants. First, to keep the program shorter I choose the following sentence: Welcome to Foothill. I created separate if statements to count each consonant and I created separate if statements not count the vowels. I then tested my for loop used to count only the consonants separately and it worked correctly. My logic was to have if statements in the vowel loop that would be true to count only vowels and in the consonant loop I set up separate if statements to create true statements to count consonants and if statements to not count the vowels. At any rate, I then put both loops into the program. The 1st for loop counts the vowels and the 2nd one counts the consonants. Well, the result is as follows: enter the word Welcome, result: 3 vowels, 5 consonants. Enter the word "to," result: 1 v., 3 c. Enter the word "Foothill," result: 3 v., 5 c. (which is correct) Enter the sentence: Welcome to Foothill. Result: 7 v, 9 c. Enter the vowel "a," result: 1 v., 2 c. Enter "az," result: 1 v., 2 c. Enter "z," result: 0 v., 1 c. I do not understand why the 1st for loop that counts vowels will add consonants when I have not entered any (ex. "a," result: 1 v., 2 c.). So the big question is: Why does the for loop that should only count vowels add in consonants where none have been entered or add extra consonants when some have been entered. That is my problem. Here is my code:
Code java:
import java.util.Scanner;
public class NewFoothill
{
public static void main(String[] args) throws Exception
{
Scanner input = new Scanner(System.in);
int count = 0;
System.out.println(" Enter the String ");
String s1 = input.nextLine();
s1 = s1.toUpperCase();
System.out.println("=====RESULT=====" + s1);
s1 = s1toLowerCase();
System.our.println("=====RESULT=====" + s1);
System.out.println(" String s1 ");
for (int i = 0; i < s1.length(); i++)
{
char c = s1.charAt(i);
if (c == 'a')
{
count++;
}
if (c == 'e')
{
count++;
}
if (c == 'i')
{
count++;
}
if (c == 'o')
{
count++;
}
if (c == 'u')
{
count++;
continue;
}
}
System.out.println(" There are " + " " + count + " " + " vowels. ");
for (int i =0; i < s1.length(); i++)
{
char c = s1.charAt(i);
if (c == 'c')
{
count++;
continue;
}
if (c == 'f')
{
count++;
continue;
}
if (c == 'w')
{
count++;
continue;
}
if (c == 'l')
{
count++;
continue;
}
if (c == 'm')
{
count++;
continue;
}
if (c == 't')
{
count++;
continue;
}
if (c == 'h')
{
count++;
continue;
}
if (c != 'a')
{
count++;
break;
}
if (c != 'e')
{
count++;
break;
}
if (c != 'i')
{
count++;
break;
}
if (c != 'o')
{
count++;
break;
}
if (c != 'u')
{
count++;
break;
}
}
System.out.println(" There are " + " " + count + " " + " consonants. ");
}
}
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
My code which I entered on 24/04/12 , 2351 hours (Thai time) compiles w/o errors. I use the Eclipse Europa. I edited my code so I hope it is now highlighted correctly and I corrected a miss spelling on String. Thanks for your help Norm.
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
Yes, this code compiles w/o errors.
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
Please post the output from the program and add comments to it describing what is wrong and show what the output should be.
Your long paragraphs hide that output if it is inside of them.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
Norm, did you receive the contents from my command prompt window along with my comments?
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
I don't see it. Where did you post it?
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
Norm, I posted it in the quick reply box. It looks like I will have to do it again. I am unable to paste to this site because I cannot get my script blocker to allow it. I will copy the command prompt here and highlight it.
Code java:
Enter the String
O
=====RESULT=====O
=====RESULT=====o
String s1
There are 1 vowels.
There are 2 consonants.
The vowel count is correct and the consonant count is not. It should be: There are 0 consonants.
Code java:
Enter the String
W
=====RESULT=====W
=====RESULT=====w
String s1
There are 0 vowels.
There are 1 consonants.
The output is 100% correct.
Code java:
Enter the String
Welcome!
=====RESULT=====WELCOME!
=====RESULT=====welcome!
String s1
There are 3 vowels.
There are 5 consonants.
The vowel count is correct. The consonant count is not. The count should be: There are 4 consonants.
Code java:
Enter the String
OW
=====RESULT=====OW
=====RESULT=====ow
String s1
There are 1 vowels.
There are 2 consonants.
The vowel count is correct. The consonant count is not. The consonant count should be: There are 1 consonants.
Code java:
Enter the String
Foothill
=====RESULT=====FOOTHILL
=====RESULT=====foothill
String s1
There are 3 vowels.
There are 5 consonants.
The answer is 100% correct.
Code java:
Enter the String
Welcome to Foothill.
=====RESULT=====WELCOME TO FOOTHILL.
=====RESULT=====welcome to foothill.
String s1
There are 7 vowels.
There are 9 consonants.
The vowel count is correct. The consonant count is not. The consonant count should be: There are 10 consonants.
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
You should play computer with your program for one of the Strings that is not counted correctly. Take a piece of paper and Manually do what the code is supposed to do step by step to see where the mistake in your logic is.
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
....edited by moderator
Try the above code u will get correct output
Try commented one also..................
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
How about helping the OP understand what his code is doing instead of spoonfeeding code with no comments or description of what it does.
Read this:
http://www.javaprogrammingforums.com...n-feeding.html
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
@divi0520, please read the forum rules and the link in Norms post. I have edited your post
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
Norm, I think the problem with my code resides within the control variable of my for loop that counts the vowels. The loop vowel count is fine, but it jumps down to the loop that counts consonants and adds a couple of them to the output. For example, if I enter 'a' my output comes back that I have 1 vowel and 2 consonants. I never entered any consonants. Furthermore, to make my consonant loop more precise I entered an if statement that states: if (c != 'a'). What do you think?
-
Re: Java for loops to count vowels/consonants usinf the logic of the main
Try debugging the code by Adding some println statements to the code to print out the values of the variables like the one that holds the current value of the count. The print out will help you see where the problem is.
Print out the value every time the variable's value is changed.