Re: Java for loops to count vowels/consonants usinf the logic of the main
Norm, I entered a println inside my for loop for vowels just after the if statement about 'a'. I ended the loop just after 'a' and put in the println. A sea of red appeared but I continued with the launch. A notice appeared saying that, " A handler conflict occurred. This may disable some commands." All is not well with my debugger now. In the past, I have not used the debugger with logic problems.
I have reread my textbook a 100 time concerning if statements. They are true or false. If I enter one vowel how can this machine state that I have entered one vowel and two consonants? In the consomnant loop I made if statements stating that the vowels were not to be counted. How can the vowel loop create two consonants out of thin air? Yes I know that I am missing something even though I have reread my texts concerning for loops and if statements. I will not give up and I will figure it out somehow and you will be the first to know. Thanks for your assistance.
Re: Java for loops to count vowels/consonants usinf the logic of the main
What is printed out by the printlns you have added?
If you get compiler errors, please copy and paste here the full text of the error messages.
Instead of having a bunch of independent if statements, you should merge them by using the && operator:
if(cond1 && cond2 && cond3) {...}
or the || operator:
if (cond1 || cond2 || cond3) { ...}
One problem you may have with independent if statements is that more than one can be true.
Re: Java for loops to count vowels/consonants usinf the logic of the main
I think you might have a copy-and-paste mistake. Are you aware that you are checking for vowels in your check for consonants? Furthermore, are you aware the you do not reset your count variable to 0 after you finish counting the vowels?
Those two things combined would account for your program giving you the result: 1 vowel 2 consonants for the input of "a".
This is not an issue your debugger will pick up, it is a logic mistake that you made while coding your if statements.
Re: Java for loops to count vowels/consonants usinf the logic of the main
aussiemcgr, I have changed my program as follows:
[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++;
continue;
}
}
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++;
continue;
}
if ( c == 'w' || c == 'l' || c == 'm' || c == 't' || c == 'f' || c == 'h')
{
count++;
}
}
System.out.println(" There are " + " " + count + " " + " consonants. ");
}
}
[/code]
You noted that I should not reset my count variable to 0 after I finish counting vowels. I believe you are refering to the control variable of my for loop that controls the count of consonants. However, I am not sure if you are talking about the "initial action", loop-continuation-condition" or the "action-after-each-iteration". If you are talking about the "initial action" my text says that it can be a list of zero or more comma-separated variable declaration statements or assignment expressions. That is where I get lost. I tried leaving the initial action out but that did not work. I do not know the correct way to change it. Should I put a number there? If so what number? Right now it is I have felt that my problem was in the control variable but I do not know how to correct it. I really do need your knowledge concerning this problem. To keep this test short I did not enter all the 21 consonants. I only entered the consonants within the test sentence: Welcome to Foothill. However I did enter all of the vowels into the loops. Again, let me thank you for your kind assistance. I have tried to figure this problem out on my own with the help of my text books, but I know I am missing something.
Re: Java for loops to count vowels/consonants usinf the logic of the main
Code :
if ( c != 'a' || c !='e' || c != 'i' || c != 'o' || c != 'u')
With OR (||) tests only one condition needs to be true for the if to be true.
c = 'e' means c != 'a'
In other words, that if will always be true.
Re: Java for loops to count vowels/consonants usinf the logic of the main
Hi Norm, I changed my if statement in the for loop concerning consonants to read as follows:
Code java:
if (( c != 'a' ) && ( c != 'e' ) && ( c != 'i' ) && ( c != 'o' ) && ( c != 'u )
{
count++;
continue;
}
console output
Code java:
Enter the String
A
=====RESULT=====A
=====RESULT=====a
String s1
There are 1 vowels.
There are 1 consonants.
console output
Code java:
Enter the String
Welcome to Foothill.
=====RESULT=====WELCOME TO FOOTHILL.
=====RESULT=====welcome to foothill.
String s1
There are 7 vowels.
There are 20 consonants.
Norm, progress has been made. In the past when I entered a vowel the output was always: 1 vowel and 2 consonants. Now I get 1 vowel and 1 consonant. The correct answer should be 1 vowel and 0 consonants.
aussiemcgr in a post noted above that you do not reset the "count variable" to 0 after you finish counting the vowels. I do not understand how to do that correctly. Is that one of my problems? Thanks again for your continued help and support.
Re: Java for loops to count vowels/consonants usinf the logic of the main
Why not have separate counters for each thing you are counting instead of using the same variable for more than one thing you are counting?
Re: Java for loops to count vowels/consonants usinf the logic of the main
You can do what Norm suggested (and use a different variable), or you can simply add the statement count = 0; after your System.out.println(" There are " + " " + count + " " + " vowels. "); statement. All that line does is sets the value of count back to 0 (which is the same as reseting it).
Re: Java for loops to count vowels/consonants usinf the logic of the main
You can try this
if ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ) {
// This checks to see if there is a vowel. For each letter checked in the loop if any of these are true than the current letter is a vowel.
// Add the code you need every time you hit a vowel.
}
else {
// If the if statement fails then the letter is obviously a constant. We dont need to check it. Just add your code to handle each constant
// in the else statement. Every time the if statement fails it will jump right to this else statement. And if the if statement is true it will skip this
// else statement.
}
Re: Java for loops to count vowels/consonants usinf the logic of the main
Aussiemcgr, I did what you suggested and all worked well concerning the proper counting of vowels and consonants. However, when I entered the target sentence (Welcome to Foothill.) the count of vowels was correct, but the count of consonants was not. My loop counted 13 consonants. There should be only 10. I checked the output for each word separately and the count was correct. Why does my target sentence not work? I will fiddle with my program again tomorrow. Thanks for your help. I am getting very close to making my program work correctly. Just a moment ago, I read another post and they mentioned that my program may be counting the white space, etc. concerning my loop that counts consonants so I got rid of the spaces and full stop in my target sentence (WelcometoFoothill) and the program worked correctly for the consonants. My vowel loop has always worked , but it seems that I need to rework my consonant loop. Any suggestions?
Re: Java for loops to count vowels/consonants usinf the logic of the main
Quote:
Originally Posted by
willie lee
I did what you suggested and all worked well concerning the proper counting of vowels and consonants. However, when I entered the target sentence (Welcome to Foothill.) the count of vowels was correct, but the count of consonants was not. My loop counted 13 consonants. There should be only 10. I checked the output for each word separately and the count was correct. Why does my target sentence not work? I will fiddle with my program again tomorrow. Thanks for your help. I am getting very close to making my program work correctly.
The problem is that the way jcrosby10 suggested spaces will be counted as consonants. If I were you I would first remove the spaces (or any other non-letter characters) from the entered String and then count the vowels. After that the only thing you need to do is subtract the number of vowels from the String's length (which after the removal will be the number of letters only). To remove the spaces (or any other character) from the String you can use the String's replaceAll method. If you google it you will find many examples of how it works.
Hope I was clear.
Re: Java for loops to count vowels/consonants usinf the logic of the main
Norm, I do not understand how I should implement a separate counter for each thing I am counting. Could you please give me one example. In the meantime, I implemented aussiemcgr's suggestion. All worked well, but the consonant count was still wrong concerning the target sentence (Welcome to Foothill.). The consonant count was 13 whereas it should have been 10. I am getting very close to making this program work correctly. I no longer get the 1 vowel and 2 consonants count which was driving me crazy. Thanks again for all the support.
Re: Java for loops to count vowels/consonants usinf the logic of the main
Quote:
I do not understand how I should implement a separate counter for each thing I am counting
Use a separate counter variable for each thing you want to count,
Code :
int cntr1 = 0; // counter for type 1
int cntr2 = 0; // counter for type 2
...
if(want to count type 1) {
cntr1++; // count type 1
}
...
if(want to count type 2) {
cntr2++; // count type 2
}
Re: Java for loops to count vowels/consonants usinf the logic of the main
Norm, thanks for your example. My program is counting white space and punct. I did the following:
[code = java]
System.out.println(" Enter the String. ");
String s1 = input.nextLine();
s1 = toUpperCase();
System.out.println("=====RESULT=====" + s1);
s1 = to LowerCase();
System.out.println("=====RESULT=====" + s1);
s1 = s1.replaceAll("\\p{Punct} + ", " ");
System.out.println("=====RESULT=====" + s1);
System.out.println(" String s1 ");
[/code]
Output
[code = java]
Enter the String
Welcome to Foothill.
=====RESULT=====WELCOME TO FOOTHILL.
=====RESULT=====welcome to foothill.
=====RESULT=====welcome to foothill.
String s1
There are 7 vowels.
There are 13 consonants.
[/code]
The replaceAll method, as I used it, does not get rid of the white space and punct. as I wanted. What did I do wrong and how can I correct it.
Re: Java for loops to count vowels/consonants usinf the logic of the main
For testing write a small (~ 10 lines) program that defines a String and uses the replaceAll() method and prints out the results. Then work on the regular expression used by the replaceAll() method until you find one that does what you want. If you have problems, Post the small program and its results with your questions.
Re: Java for loops to count vowels/consonants usinf the logic of the main
Hi Norm, I finally got the program to work correctly.
Code java:
String s1;
System.out.println(" Enter the String ");
s1 = input.nextLine();
s1 = toUpperCase();
System.out.println("=====RESULT=====" + s1);
s1 = toLowerCase();
System.out.println("=====RESULT=====" + s1);
String nonStrippedString = s1;
s1 = nonStrippedString.replace( ".", "");
s1 = s1.replaceAll(" [^A-Za-z]", "");
System.out.println(" String s1 ");
Enter the String
Welcome to Foothill.
=====RESULT=====WELCOME TO FOOTHILL.
=====RESULT=====welcome to foothill.
String s1
There are 7 vowels.
There are 10 consonants.
I have tested it by including different symbols and numbers. The vowels and consonants are counted correctly. Now, I must display the String in the reverse order. If I get stuck I will contact you for assistance. Again, thanks to you and all the other interested parties. I would put the solved symbol on this program, but I do not see it on this page.
Re: Java for loops to count vowels/consonants usinf the logic of the main
Just try using && instead of || in the second for loop.I think that should solve a major part of the problem.