简体   繁体   中英

binary to decimal converter without using parseint or arrays

Getting string index out of range but I don't understand why I've went through it about 50 times

import java.util.Scanner;
public class binary {

    public static void main(String[] args) {
        System.out.println("Enter the first binary number");
        Scanner keyboard = new Scanner(System.in);
        String num1 = keyboard.next();
        //System.out.println("Enter the second binary number");
        //String num2 = keyboard.next();
        
        int total = 0;
        for(int i = num1.length(); i>0;i--) {
            if(num1.charAt(i) == 1) {
                total += 2*i;
            }
            
        }
        if(num1.charAt(3) == 1) {
            total -= 1;
        }
        System.out.println(total);

    }

}

Here's a complete solution to what you're trying to do, including a set of tests:

class binary {

    private static int binaryToInt(String binary) {
        int total = 0;
        for (int i = 0 ; i < binary.length(); i++) {
            total *= 2;
            if (binary.charAt(i) == '1')
                total += 1;
        }
        return total;
    }

    private static void test(String binary, int expected) {
        int n = binaryToInt(binary);
        String rightWrong = "right";
        if (n != expected) {
            rightWrong = String.format("WRONG! (should be %d)", expected);
        System.out.printf("%s -> %d is %s\n", binary, n, rightWrong);
    }

    public static void main(String[] args) {
        test("0", 0);
        test("1", 1);
        test("10", 2);
        test("100", 4);
        test("111", 7);
        test("0000111", 7);
        test("1010101010", 682);
        test("1111111111", 1023);

        System.out.println("");

        // test sanity check
        System.out.println("This last test should fail (we are just testing the test method itself here)...");
        test("1010101010", 0);
    }
}

Result:

0 -> 0 is right
1 -> 1 is right
10 -> 2 is right
100 -> 4 is right
111 -> 7 is right
0000111 -> 7 is right
1010101010 -> 682 is right
1111111111 -> 1023 is right

This last test should fail (we are just testing the test method itself here)...
1010101010 -> 682 is WRONG! (should be 0)

One significant problem in your code hasn't yet been addressed in the comments or earlier answers. Note this line vs the one in your code:

if (binary.charAt(i) == '1')

You were testing for the numeric value 1 , which is never going to be true because you're getting back a character from charAt() , not a number.

While length() counts the number of elements, their indexes start at 0. For a string of "1111" the last character would be at index 3, not 4, so .length()-1 . You would need to either change your for statement to for(int i = num1.length()-1; i>=0;i--) (notice also the condition change) or change the charAt statement to if(num1.charAt(i-1) == '1') .

Also, based on what you are trying to do, I assume for total += 2*i you actually need something like total += Math.pow(2, i-length()) depending on what you decide to do with i first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM