Hello World
Hello World

Reputation: 213

How to fix this Hexadecimal to Binary Converter?

I have the following code. The issue is that the code is not working when I enter 0-9 characters. Where is the problem?

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    // Enter a hex digit
    System.out.print("Enter a hex digit: ");
    String hexString = input.nextLine();

    // Enter one character
    if (hexString.length() != 1) {
        System.out.println("You must enter exactly one character");
    }

    // Display binary number for the hex digit
    char hexchar = hexString.charAt(0);

    if ((hexchar >= 'A' && hexchar <= 'F') || (hexchar >= 0 && hexchar <= 9))
    {
        System.out.print("The binary value is ");
        switch(hexchar)
        {
            case 0: System.out.println(0000); break; 
            case 1: System.out.println(0001); break; 
            case 2: System.out.println(0010); break; 
            case 3: System.out.println(0011); break; 
            case 4: System.out.println(0100); break; 
            case 5: System.out.println(0101); break; 
            case 6: System.out.println(0110); break; 
            case 7: System.out.println(0111); break; 
            case 8: System.out.println(1000); break; 
            case 9: System.out.println(1001); break; 
            case 'A': System.out.println(1010); break; 
            case 'B': System.out.println(1011); break; 
            case 'C': System.out.println(1100); break; 
            case 'D': System.out.println(1101); break; 
            case 'E': System.out.println(1110); break; 
            case 'F': System.out.println(1111); break; 
        }
    }
    else
        System.out.println(hexchar + " is and invalid input");

    }
}

I expect the code only accept one digit, and then convert it to the binary number. If not, show an error, or add a Handling Exception

EDIT: (WORKING)

    if ((hexchar >= 'A' && hexchar <= 'F') || (hexchar >= '0' && hexchar <= '9'))
    {
        System.out.print("The binary value is ");
        switch(hexchar)
        {
            case '0': System.out.println("0000"); break; 
            case '1': System.out.println("0001"); break; 
            case '2': System.out.println("0010"); break; 
            case '3': System.out.println("0011"); break; 
            case '4': System.out.println("0100"); break; 
            case '5': System.out.println("0101"); break; 
            case '6': System.out.println("0110"); break; 
            case '7': System.out.println("0111"); break; 
            case '8': System.out.println("1000"); break; 
            case '9': System.out.println("1001"); break; 
            case 'A': System.out.println("1010"); break; 
            case 'B': System.out.println("1011"); break; 
            case 'C': System.out.println("1100"); break; 
            case 'D': System.out.println("1101"); break; 
            case 'E': System.out.println("1110"); break; 
            case 'F': System.out.println("1111"); break; 
        }

Now I am editing the code for errors and to make it a little better. Thank you very much to everyone who helped.

Upvotes: 0

Views: 85

Answers (2)

wilmol
wilmol

Reputation: 1900

Because hexchar is a char.

You need to check '0', '1', '2' etc. not 0, 1, 2.


Btw switch case statement has a default keyword, use that instead of the if else.

Can then avoid the 'valid' character checking as all other input will go to the default case.


Also binary numbers should lead with 0b (if int).

If you want to print the binary number put quotes around it e.g. "0011", so it forces the String you want.

Upvotes: 4

Rafael Aquino
Rafael Aquino

Reputation: 11

Gabriel. Here you are getting the first character of a String. for example "hola" -> 'h'
char hexchar = hexString.charAt(0);

You should compare case '0': System.out.println(0000); break; case '1': etc.

Also need change this part (hexchar >= 0 && hexchar <= 9) to (hexchar >= '0' && hexchar <= '9')

Upvotes: 1

Related Questions