开发者

if statement not executing

for(int i=0;i<number.length();i++){
    if(number.charAt(i)==0){
      nums[i]=11;
      System.out.println("bob");
开发者_JS百科    }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?


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜