0

This program should check the string you input for vowels and then output the amount of vowels. However, when I run the program, the counter displays 0. There are no errors being displayed by Eclipse.

I would expect that if I type in hello then it would display 2, but it always displays 0.

import javax.swing.*;
public class Lab5 {
  public static boolean isVowel(char x) {
    if (x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U') {
        return true;
    }
    else {
        return false;
    }
  }
  public static void main(String[] args) {
    String seq;
    boolean vowel;
    int counter = 0;
    seq = JOptionPane.showInputDialog(null, "Please enter a sequence of characters: ");
    seq.toUpperCase();
    for (int i=0; i<seq.length(); i++) {
        vowel = isVowel(seq.charAt(i));
        if (vowel == true) {
            counter++;
        }   
    }
    JOptionPane.showMessageDialog(null, "The number of vowels in your sequence of characters is : " + counter);
  }
}
AstroCB
  • 12,337
  • 20
  • 57
  • 73
prograded
  • 13
  • 2
  • You're starting to attract close votes for the question not being clear enough on desired behaviour. I've edited to clarify. The close-vote brigade is quite hard to satisfy sometimes. – chiastic-security Oct 05 '14 at 23:42
  • 1
    @dasblinkenlight This doesn't seem like a duplicate *question* to me. It might have the same *answer* as another question does, but that's a different matter. The question "What is 2+2?" is not a duplicate of "What is 3+1?". – chiastic-security Oct 05 '14 at 23:50
  • @chiastic-security - *'The question "What is 2+2?" is not a duplicate of "What is 3+"'*. Literally, you are correct. But one purpose of "close as duplicate" is to avoid StackOverflow being cluttered up with zillions of Q&A's that are *almost* the same, even if they are not literal duplicates. And if you read the linked Q&A, it does give the solution to the OP's problem. – Stephen C Oct 05 '14 at 23:56
  • @StephenC I really meant that the questions are totally different, even if the answers are the same. I realise that where the *questions* are almost the same then it is enough to warrant closure, but I don't think that's true here. – chiastic-security Oct 05 '14 at 23:58
  • 1
    @chiastic-security - The code is different. The root problem is the same. That is sufficient to warrant closure, IMO. If you don't like it, take this to the SO "meta" site. (But I don't fancy your chances of convincing people.) – Stephen C Oct 06 '14 at 00:00

2 Answers2

5

It's because seq.toUpperCase() doesn't do what you think it does. It returns a copy of the String, converted to upper case, but it doesn't alter the original. (A String is immutable in Java, so it can't alter it.)

Try changing that line to

seq = seq.toUpperCase();
chiastic-security
  • 20,430
  • 4
  • 39
  • 67
3

Strings are immutable in Java, so you can't modify them in place. When you call toUpperCase() on a string, it will return a copy of that string with all the characters changed to their upper-case equivalent, but the original string is left unchanged.

This means that you have to assign the result of calling toUpperCase to your variable again. So, instead of

seq.toUpperCase();

Do

seq = seq.toUpperCase();
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156