0

Im trying to add a counter every time a string has a vowel in it with a for loop that only runs with alloted test cases but im getting the wrong output.

public class Prob03 {
    public static void main(String[] args) {
        int count = 0;
        Scanner scan = new Scanner(System.in);
        int runs = scan.nextInt();
        
        for (int l = 0; l < runs; l++) {
            String sentence = scan.nextLine().toLowerCase();
            sentence = "\n" + sentence;
            for (int i = 0; i < sentence.length(); i++) {
                char vowels = sentence.charAt(i);
                if (vowels == 'a' || vowels == 'e' || vowels == 'i' || vowels == 'o' || vowels == 'u') {
                    count++;
                }
            }
            System.out.println(count);
        }
    }
}
ib.
  • 25,324
  • 10
  • 71
  • 97
  • 2
    `sentence = "\n" + sentence;` - why? – Scary Wombat Oct 02 '20 at 00:43
  • Consume the newline passed when entering your integer value with **nextInt()**. For example: `int runs = scan.nextInt(); scan.nextLine();` otherwise that newline character will automatically be applied to your following `String sentence = scan.nextLine().toLowerCase();` – DevilsHnd Oct 02 '20 at 00:51
  • What have you tried to debug the problem? – Nico Haase Oct 02 '20 at 10:26

0 Answers0