1

I'm making a program that counts the number of vowels in a phrase that was assigned by my teacher. To do this I have made a for-loop that in theory should check each character for being a, then e, the i, and so on before moving on to the next character. For some reason though nothing in the for-loop works. The build output states that everything is fine, but the for-statements aren't functioning properly. I know that t actually is the correct letter because I printed what t was but the for loop still won't work. What confuses me is the build output is fine, so it must be a logic error, but I can't find where it is. Here is the code:

for (i = 0; i != phrase.length(); i++) {
    String t = phrase.substring(i, i + 1);

    if (t == "a") {
        count++;
        System.out.println(count);
    }
    if (t == "e") {
        count++;
    }
    if (t == "i") {
        count++;
    }
    if (t == "o") {
        count++;
    }
    if (t == "u") {
        count++;
    }
}

System.out.println(count);

Many thanks to anyone who can help me!

Ry-
  • 218,210
  • 55
  • 464
  • 476

9 Answers9

4

the exit condition is wrong, try:

for (i = 0 ; i < phrase.length(); i++) {
Emilio Rodriguez
  • 5,709
  • 4
  • 27
  • 32
2

You have several issues in your code.

1) First issue being the i == phrase.length(). This is the until condition. Change this to i <= phrase.length(). *

In your case this would have worked but it's better to do it right. Normally you check on values here that could for example grow bigger than 10 and if you skip 10 and keep counting you would enter an infinite loop. So always use <= or >=.

*edit: Correction you should use < since you have a substring working with i+1.

2) Don't use == for String (or any other object) comparison. That will compare the memory address instead. For String use the equals() method.

Also make sure to put the known literal "a", "b" and so up front since you could get a nullpointer exception otherwise.

3) It might be better to go for a charAt(0) first and then use == since you can compare char (primitive) values.

4) I also combined the if statements in one.

String phrase = "WazzUp ? Who’s On FIRST??? IDUNNO";
phrase = phrase.toLowerCase();
System.out.println("value of phrase is: "+phrase);

int count = 0;

for (int i = 0 ; i < phrase.length() ; i++) {

  String t = phrase.substring(i, i + 1);

  if ("a".equals(t) || "e".equals(t) || "i".equals(t) || "o".equals(t) || "u".equals(t) ) {
    count++;
  }

}

System.out.println("value of count is: "+count);
hcpl
  • 17,382
  • 7
  • 72
  • 73
1

Check the for exit condition and String comparison, which should be done using equal: if ("a".equals(t)) { ... }.

Smutje
  • 17,733
  • 4
  • 24
  • 41
1

Your for-decleration is wrong.

It should be

for(<startexpression> ; <any expression, that is still true> ; in-/decrement>)

In your case you start with i=0, so on the same run i cannot equal phrase.length()in your case. This is why you code does not get executet.

Try:

for (i = 0 ; i < phrase.length(); i++) {

instead.

Also, when comparing string, you should use

String.equals("whatever")

instead of the equeals-operator (=).

LuigiEdlCarno
  • 2,410
  • 2
  • 21
  • 37
0

In your for loop, shouldn't it be i = phrase.length() instead of i ==? i == is doing comparison instead of setting i equal to (i = sets it equal to something instead of comparing)

dario_ramos
  • 7,118
  • 9
  • 61
  • 108
Stc5097
  • 291
  • 1
  • 11
  • 25
0

As to what emilioicai said your conditions is wrong

for (i = 0 ; i == phrase.length(); i++) { <----! Wrong

for (i = 0 ; i < phrase.length(); i++) { <---- Correct 

Also strings Should be compared with equals, so instead of this

if (t == "a") {
        count++;
        System.out.println(count);
    }

You should have this

if ("a".equals(t)) {
        count++;
        System.out.println(count);
    }

And if you want to go fancy you can do something like this :)

String phrase = "WazzUp ? Who’s On FIRST??? IDUNNO";
phrase = phrase.toLowerCase();
System.out.println("value of phrase is: "+phrase);

int count = 0;
String[] characters = new String[] {"a", "e", "i", "o", "u"};
    for (int i = 0 ; i < phrase.length() ; i++) {       
        String t = phrase.substring(i, i + 1);      
        for(String character : characters){
            if(character.equals(t)){
                count++;
            }
        }
    }
System.out.println("value of count is: "+count);

The above makes code nicer, more scalable, and easier to read

Maciej Cygan
  • 5,351
  • 5
  • 38
  • 72
0

There are two errors about your code. First, for (i = 0 ; i < phrase.length(); i++) {} Second, if (t .equals("a")) { but not if (t == "a") {

easynoder
  • 104
  • 4
0

try this:

for (i = 0; i != phrase.length(); i++) {

String t = phrase.substring(i, i + 1);
System.out.println(t);

if (t.equals("a")) {
    count++;
    System.out.println(count);
}
if (t.equals("e")) {
    count++;
}
if (t.equals("i")) {
    count++;
}
if (t.equals("o")) {
    count++;
}
if (t.equals("u")) {
    count++;
}

}

by the way, it's more common to change your condition to this:

i < phrase.length()

since it shows the progress better and increases the readability of your code.

Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
-1

Make it this way

String phrase = "WazzUp ? Who’s On FIRST??? IDUNNO";
phrase = phrase.toLowerCase();

int count = 0;

int i = 0;
 System.out.println(phrase);

for (i = 0 ; i < phrase.length(); i++) {

String t = phrase.substring(i, i + 1);

if (t == "a") {
  count++;
  System.out.println(count);
  }
if (t == "e") {
  count++;
  }
if (t == "i") {
  count++;
  }
if (t == "o") {
  count++;
  }
if (t == "u") {
  count++;
  }

  }

  System.out.println(count);
MARKAND Bhatt
  • 2,428
  • 10
  • 47
  • 80