armitage
armitage

Reputation: 85

if statement not executing

for(int i=0;i<number.length();i++){
    if(number.charAt(i)==0){
      nums[i]=11;
      System.out.println("bob");
    }else{
    nums[i]=number.charAt(i);
  }
  }

I am trying to get all 0 values to equal 11 in the array of nums and print bob every time it does that but for some reason the first if statement doesnt seem to be executing even when charAt(i) equals 0. Could someone please explain what is wrong?

Upvotes: 1

Views: 483

Answers (3)

Jesus Ramos
Jesus Ramos

Reputation: 23266

If this is a string don't you mean if (number.charAt(i) == '0') ?

Upvotes: 4

Belgi
Belgi

Reputation: 15082

try : if(number.charAt(i)=='0'). you compared int and char my mistake.

Upvotes: 1

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236150

You should ask:

if (number.charAt(i)=='0')

Because the number 0 is different from the character '0', and you're interested in the character.

Upvotes: 4

Related Questions