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.
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. "); } }