So the program I'm writing should count how many of each individual vowel is in the program and then print that.

I'm basically stuck with while and conditional statements. I think my if statement is where I've gone wrong. I'm not sure how to have it read the letter properly and the program didn't compile and I'm not sure why.

Please help! Thank you.

//Project 4.12 by Vivin Viswanathan

import java.util.Scanner;

public class VV_PP412
    {
    public static void main (String[]args)
        {
        int cons = 0;
        int a,e,i,o,u = 0;
        int count = 0;
        String sent;

        Scanner scan = new Scanner (System.in);
        System.out.println("Type out a random string of BS: ");

        sent = scan.nextLine();

        while (sent.charAt(count) != sent.charAt(sent.length()))
            {
            count++;
            if (sent.charAt(count) == char 'a')
                a++;
            }
        }
    }

Edited 4 Years Ago by peter_budo: Please do not use code snippets to post questions

if (sent.charAt(count) == char 'a')

this should be

if (sent.charAt(count) == 'a')

and ... also, you may want to print out the values at the end of your main method, to check if your counter worked as you want it to.

while (sent.charAt(count) != sent.charAt(sent.length()))

This is presumably to limit the loop to the length of the string, but will fail if the last letter is repeated somewhere else in the sring, eg for "abcb" the loop will terminate on the second letter ('b') because it matches the last letter (also 'b'). Just test the value of count against the length of the string.

Why not use a switch statement for the vowels nested in the while loop?

This article has been dead for over six months. Start a new discussion instead.