2

I am writing a program that converts between decimal, binary, and hexadecimal. The program compiles, but when I go to enter input the binary to hexadecimal I get an exception "java.lang.StringIndexOutOfBoundsException: String index out of range: 34". That is the first issue. The second issue is that the hexadecimal to binary conversion gives me some ridiculously lengthy (and incorrect) return. I have included the code for both. An nudge in the right direction is greatly appreciated.

Binary to Hexadecimal:

/**
 * Method that converts a binary number to its hexadecimal equivalent.
 * @param no parameters
 * @return returns void
 */
public void binToHex()
{

    System.out.println("The binary number you enter will be converted to its hexidecimal equivalent.");

    System.out.println("Please enter a binary number: ");

    Scanner keyboard = new Scanner(System.in);

    String bin = keyboard.nextLine();

    String oldbin = bin;

    bin = bin.replace(" ", "").trim();

    StringBuffer hex = new StringBuffer("00000000000000000000000000000000");

    //String hex1 = "";

    int j = 0;

    for (int i = 0; i < bin.length(); i++)
    {

        if (bin.substring(i, i+4).equals("0000"))
        {

            hex.setCharAt(j, '0');
        }

        else if (bin.substring(i, i+4).equals("0001"))
        {

            hex.setCharAt(j, '1');
        }

        else if (bin.substring(i, i+4).equals("0010"))
        {

            hex.setCharAt(j, '2');
        }

        else if (bin.substring(i, i+4).equals("0011"))
        {

            hex.setCharAt(j, '3');
        }

        else if (bin.substring(i, i+4).equals("0100"))
        {

            hex.setCharAt(j, '4');
        }

        else if (bin.substring(i, i+4).equals("0101"))
        {

            hex.setCharAt(j, '5');
        }

        else if (bin.substring(i, i+4).equals("0110"))
        {

            hex.setCharAt(j, '6');
        }

        else if (bin.substring(i, i+4).equals("0111"))
        {

            hex.setCharAt(j, '7');
        }

        else if (bin.substring(i, i+4).equals("1000"))
        {

            hex.setCharAt(j, '8');
        }

        else if (bin.substring(i, i+4).equals("1001"))
        {

            hex.setCharAt(j, '9');
        }

        else if (bin.substring(i, i+4).equals("1010"))
        {

            hex.setCharAt(j, 'A');
        }

        else if (bin.substring(i, i+4).equals("1011"))
        {

            hex.setCharAt(j, 'B');
        }

        else if (bin.substring(i, i+4).equals("1100"))
        {

            hex.setCharAt(j, 'C');
        }

        else if (bin.substring(i, i+4).equals("1101"))
        {

            hex.setCharAt(j, 'D');
        }

        else if (bin.substring(i, i+4).equals("1110"))
        {

            hex.setCharAt(j, 'E');
        }

        else if(bin.substring(i, i+4).equals("1111"))
        {

            hex.setCharAt(j, 'F');
        }

        i = i + 4;

        j = j + 1;
    }

    System.out.println("The binary number you entered, " + oldbin + " is " + hex + " in hexadecimal.\n");

    pw.print("The binary number you entered, " + oldbin + " is " + hex + " in hexadecimal.\n");
}

}

Hexadecimal to binary:

/**
 * Method that converts a hexadecimal number to its binary equivalent.
 * @param no parameters
 * @return returns void
 */
public void hexToBin()
{

    System.out.println("The hexadecimal number you enter will be convered to its binary equivalent.");

    System.out.println("Please enter a hexadecimal number: ");

    Scanner keyboard = new Scanner(System.in);

    String bin = keyboard.nextLine();

    bin = bin.trim();

    String binary = "";

    for (int i = 0; i < bin.length(); i++)
    {

        if(bin.charAt(i) == '0')
        {

            binary = binary.concat("0000");
        }

        else if(bin.charAt(i) == '1')
        {

            binary = binary.concat("0001");
        }

        else if(bin.charAt(i) == '2')
        {

            binary = binary.concat("0010");
        }

        else if(bin.charAt(i) == '3')
        {

            binary = binary.concat("0011");
        }

        else if(bin.charAt(i) == '4')
        {

            binary = binary.concat("0100");
        }

        else if(bin.charAt(i) == '5')
        {

            binary = binary.concat("0101");
        }

        else if(bin.charAt(i) == '6')
        {

            binary = binary.concat("0110");
        }

        else if(bin.charAt(i) == '7')
        {

            binary = binary.concat("0111");
        }

        else if(bin.charAt(i) == '8')
        {

            binary = binary.concat("1000");
        }

        else if(bin.charAt(i) == '9')
        {

            binary = binary.concat("1001");
        }

        else if(bin.charAt(i) == 'A');
        {

            binary = binary.concat("1010");
        }

        if(bin.charAt(i) == 'B');
        {

            binary = binary.concat("1011");
        }

        if(bin.charAt(i) == 'C');
        {

            binary = binary.concat("1100");
        }

        if(bin.charAt(i) == 'D');
        {

            binary = binary.concat("1101");
        }

        if(bin.charAt(i) == 'E');
        {

            binary = binary.concat("1110");
        }

        if(bin.charAt(i) == 'F');
        {

            binary = binary.concat("1111");
        }
    }

    System.out.println("The hexadecimal you entered, " + bin + " is " + binary + " in binary.\n");

    pw.print("The hexadecimal you entered, " + bin + " is " + binary + " in binary.\n");
}

}

5
  • 1
    Why not use toBinaryString(), toHexString() and the parseInt() that takes a radix? Commented Sep 5, 2014 at 19:31
  • Starting with the 'A' case, you have extra semicolons immediately following the else if conditions that should be removed. Commented Sep 5, 2014 at 19:36
  • This is for an assignment and I am not allowed to use any of Java's built-in library functions to do the conversion. Commented Sep 5, 2014 at 19:37
  • Step through the code with a debugger or insert println statements to find out what's going on with the StringIndexOutOfBounds error. This is a simple debugging task and something you need to learn to do. Note that the exception stack trace tells you exactly where you're getting this error, so it's not an Easter egg hunt. Commented Sep 5, 2014 at 20:00
  • I was able to figure it out. Now I have a different issue that I can't seem to solve. Thank you for all your help! Commented Sep 5, 2014 at 21:51

1 Answer 1

2

In binToHex :

The first problem is that in bin.substring(i, i+4) , i+4 may be out of bounds, since i gets as high as bin.length()-1.

THe second problem is the you don't know that the binary String is divisable by 4. You should left pad it with zeroes to make it so.

After you do the left padding, you can change the loop to :

for (int i = 0; i < bin.length(); i+=4)

Then bin.substring(i, i+4) will never be out of bounds.

EDIT : I just noticed that you do increment i by 4 at the end of each iteration. However, you also increment i by 1 in the for loop, so in total you increment it by 5 in each iteration.

I would also suggest the you use StringBuilder instead of StringBuffer (you don't need thread safety). And use the append method to add characters to it. And don't use String.concat for the second method. Use StringBuilder.

In hexToBin :

You forgot else before some of your ifs. and you have ";" after some of the conditions.

For example : if(bin.charAt(i) == 'B');

This means the code following that condition would always be executed.

The correct implementation would be (though I still suggest to use StringBuilder instead) :

    if(bin.charAt(i) == '0')
    {

        binary = binary.concat("0000");
    }

    else if(bin.charAt(i) == '1')
    {

        binary = binary.concat("0001");
    }

    else if(bin.charAt(i) == '2')
    {

        binary = binary.concat("0010");
    }

    else if(bin.charAt(i) == '3')
    {

        binary = binary.concat("0011");
    }

    else if(bin.charAt(i) == '4')
    {

        binary = binary.concat("0100");
    }

    else if(bin.charAt(i) == '5')
    {

        binary = binary.concat("0101");
    }

    else if(bin.charAt(i) == '6')
    {

        binary = binary.concat("0110");
    }

    else if(bin.charAt(i) == '7')
    {

        binary = binary.concat("0111");
    }

    else if(bin.charAt(i) == '8')
    {

        binary = binary.concat("1000");
    }

    else if(bin.charAt(i) == '9')
    {

        binary = binary.concat("1001");
    }

    else if(bin.charAt(i) == 'A')
    {

        binary = binary.concat("1010");
    }

    else if(bin.charAt(i) == 'B')
    {

        binary = binary.concat("1011");
    }

    else if(bin.charAt(i) == 'C')
    {

        binary = binary.concat("1100");
    }

    else if(bin.charAt(i) == 'D')
    {

        binary = binary.concat("1101");
    }

    else if(bin.charAt(i) == 'E')
    {

        binary = binary.concat("1110");
    }

    else if(bin.charAt(i) == 'F')
    {

        binary = binary.concat("1111");
    }
Sign up to request clarification or add additional context in comments.

4 Comments

What about the Hex to Binary conversion? Any idea why my output is almost 3 lines long? Thank you!
@user3727648 It would help if you show the input you entered and the output you got.
The hexadecimal number you enter will be convered to its binary equivalent. Please enter a hexadecimal number: 12345678 The hexadecimal you entered, 12345678 is 00011010101111001101111011110010101010111100110111101111001110101011110011011110111101001010101111001101111011110101101010111100110111101111011010101011110011011110111101111010101111001101111011111000101010111100110111101111 in binary.
Yes, I noticed that as well and I fixed it (that was a really stupid error on my part inserting ";" where they should not have been). The hexadecimal to binary conversion works just fine now. Still refining the other code and appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.