Counting Vowels and Consonants in a String.
Hello everyone, I am so close to achieving my goal with this program. I just can't seam to figure out where the for loop goes wrong and "mis-counts" the amount of consonants and vowels. Also, in the else...if statement if anyone has advise to make it so it excludes all characters (like !, -, ...etc) besides vowels that would help so much! Part of the consonant problem I believe is it is counting spaces and other characters maybe?
Thanks so much in advance for any help!
Code Java:
import java.lang.String;
public class StringProcessor
{
String string;
int vowels;
int consonants;
public void Count()
{
vowels = 0;
consonants = 0;
for(int i = 0; i < string.length(); i++)
{
char c = string.charAt(i);
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
vowels++;
else
if(c != 'a' || c != 'e' || c != 'i' || c != 'o' || c != 'u' || c != ' ')
consonants++;
}
}
public void display()
{
System.out.println(string + " has " + vowels + " vowels and "
+ consonants + " consonants.");
}
public StringProcessor(String aString)
{
string = aString;
}
public void setString(String stString)
{
string = stString;
}
}
public class TestProcessor
{
public static void main(String[] args)
{
StringProcessor processor = new StringProcessor("Java Rules");
processor.Count();
processor.display();
processor.setString("I like on-line classes");
processor.Count();
processor.display();
processor.setString("Spring break was AWESOME!");
processor.Count();
processor.display();
}
}
The output is supposed to look like this:
"Java Rules" has 4 vowels and 5 consonants
"I like on-line classes" has 8 vowels and 10 consonants
"Spring break was AWESOME!" has 8 vowels and 13 consonants
And I get this:
Java Rules has 4 vowels and 6 consonants.
I like on-line classes has 7 vowels and 15 consonants.
Spring break was AWESOME! has 4 vowels and 21 consonants.
Thanks again!
Re: Counting Vowels and Consonants in a String.
Go through your for loop character by character, tracing through what's going to happen in each case. Think about it. What happens when c == ' '?
Also, wouldn't it just be easier to have a List (or even a String) that contains all of the vowels, and use that to check?
Also also, you might want to check out the methods in the Character class. The API is your friend.
Re: Counting Vowels and Consonants in a String.
Thanks man, now do you mind me asking, are you saying in my else...if for consonants I should set c == 'b' c == 'c' and so on and so forth for EVERY consonant? Also, how would I go about doing:
"Also, wouldn't it just be easier to have a List (or even a String) that contains all of the vowels, and use that to check?"
You wouldn't mind just providing an example of how you can use a "list" to check if it's a vowel or consonant?
Thanks so much!
Re: Counting Vowels and Consonants in a String.
Quote:
Originally Posted by
Andyandhisboard
Thanks man, now do you mind me asking, are you saying in my else...if for consonants I should set c == 'b' c == 'c' and so on and so forth for EVERY consonant?
Not at all. I'm saying you should run through that code, especially at that line, to see what's going on. Pay close attention to what happens for the space character.
Quote:
Originally Posted by
Andyandhisboard
You wouldn't mind just providing an example of how you can use a "list" to check if it's a vowel or consonant?
Here's some pseudo-code that checks to see if an animal is in a zoo:
Code java:
List<String> animals = new ArrayList<String>();
animals.add("Tiger");
animals.add("Penguin");
System.out.println("Is a Tiger in the zoo: " + animals.contains("Tiger")); //true
System.out.println("Is a Lion in the zoo: " + animals.contains("Lion")); //false
Re: Counting Vowels and Consonants in a String.
Thanks again :D
I'm afraid I don't understand what happens when I compare c == " ". My guess would be maybe the space would represent any character that is a part of the string?
Also, so if I were to make a list like the following? How would I compare it to the string that were testing from the getString method?
Code Java:
List<String> vowel = new ArrayList<String>();
vowel.add('a');
vowel.add('e');
vowel.add('i');
vowel.add('o');
vowel.add('u');
And thanks so much for your help, I really do appreciate it!!!
Re: Counting Vowels and Consonants in a String.
Quote:
Originally Posted by
Andyandhisboard
I'm afraid I don't understand what happens when I compare c == " ". My guess would be maybe the space would represent any character that is a part of the string?
Nope. And there is no need for guessing- simply trace through your program, by hand or with a debugger, and see what it's doing on that line. Hint- what happens when the space character (or any non-vowel) is tested against the first condition (being not equal to 'a')?
Quote:
Originally Posted by
Andyandhisboard
Also, so if I were to make a list like the following? How would I compare it to the string that were testing from the getString method?
Sort of, but you might want to use a List of Characters instead of Strings. Up to you. And you'd test it the same way, by each character. Only instead of comparing the characters in the test String separately to each vowel, you'd simply test whether it was in the List. It just tidies up your if statements.
Re: Counting Vowels and Consonants in a String.
Wouldn't it be i <= string.length() in your for loop parameters, as you're not including the last character in your string (if I'm not mistaken)
Re: Counting Vowels and Consonants in a String.
No, you are mistaken. Strings, like arrays, are zero based. So for a String with a length of 5, characters exist at indicies of 0-4. Length, that is 5, would be invalid.
Re: Counting Vowels and Consonants in a String.
Hey OP, did you get this figured out? You were pretty close yesterday, and I'm just wondering if you ever figured out what I was getting at.
Re: Counting Vowels and Consonants in a String.
first of all it s better if you convert all letters to uppercase or to lowcase. This fixes one logic problem. After that erase the second if and replace with something like if(c != ' '). Make conditions for every non-vowel character. I hope this help you
Re: Counting Vowels and Consonants in a String.
Gotta love spoonfeeding...
Re: Counting Vowels and Consonants in a String.
Quote:
Originally Posted by
Junky
No, you are mistaken. Strings, like arrays, are zero based. So for a String with a length of 5, characters exist at indicies of 0-4. Length, that is 5, would be invalid.
Ah yes, thank you for alerting me about that.
Re: Counting Vowels and Consonants in a String.
the miscount is due to the for loop you need a <= instead of a < otherwise it will stop counting one chracter before the end. I found your program/applet ver useful. Thank you.
Re: Counting Vowels and Consonants in a String.
@JavaWiz You're a little late posting on a 22 month old thread.
Quote:
the for loop you need a <= instead
That usually causes the index to go past the end of the array. The index for the last element in the array is array length-1
If you're not sure, write a small simple program that scans a String and prints out each character in the String.
Re: Counting Vowels and Consonants in a String.
Srry fail i just relized why it had to be the other way ... Fail