public class BinaryToDecimalRecursive { public static void main(String[] args) { int decimal = toDecimal("10"); } public static int toDecimal(String str) { System.out.println(str.length()); // this was printing 3 before! if (str.length() == 1) { return Integer.parseInt(str); } else { return 0; } } }
as i was playing with my BinaryToDecimal recursive program, i bumped into something that makes me confuse, the code above is something i did to reproduce the error i encountered a while ago, but i was unable to produce it again, as you can see im passing a String argument with ofcourse(obviously) has a length of 2, but the weird thing i saw a while ago, when i tried to print its length, it gave me a length of 3, how come? ichecked and checked it, create another program where the length method call returns a valid string length, i close the code tab, ran some other programs, go back to this one again, then boom! it gave me a length of 2. Im sorry but i cant reproduce it again, im just curious why did it happen.