java - Not Entering an if Condition even if statements are valid -
my error in code though "a" , "b", "c" exist in array if condition inside inner loop doesn't recognize them. note abc string has been splitted using string.split(""); method of java
public class hexadecimal { public static void main(string[] args) { string hex = "abc"; htod(hex); } public static void htod(string hexa) { string[] hexadecimal = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}; string[] value = hexa.split(""); int deci = 0; int bit =0; for(int = 0; i<value.length;i++) { for(int j = 0; j<hexadecimal.length;j++) { if(value[i] == hexadecimal[j]) { deci = deci + compute(j,bit++); system.out.println(deci); } } } } public static int compute(int digit,int bit) { int ans = 0; if(bit == 0) { ans = digit * (1); }else { ans = digit * (16 * bit); } return ans; } }
never compare strings ==. s1.equals(s2).
the former tests both objects same (i.e. variables reference same string object). latter tests sequences of chars of both strings same.
Comments
Post a Comment