Question
Write a program to convert a 4 digit hexadecimal number into decimal. The user should be instructed to enter a 4-digit hexadecimal number. The corresponding
Write a program to convert a 4 digit hexadecimal number into decimal. The user should be instructed to enter a 4-digit hexadecimal number. The corresponding decimal number (0 to 65,535) should be output. Note that the place values for a 4-digit hexadecimal number from left to right are 4096, 256, then 16, then 1. You must input the Hexadecimal number as a String, then use the charAt(N) method to compare each individual character to 'A', 'B', 'C', 'D', 'E', 'F' and '0' through '9', and add the value of the digit times the corresponding place value to the total.
For example, 1A3016 = 1*4096+10*256+3*16+0*1 = 670410
First, prompt for and input a String of four characters. If the input is not four characters, print out an appropriate message and exit.
Use if statements, each meaning something like "If the second character is equal to 'A', the value of the second digit is 256 * 10". If any character is not '0'-'9' or 'A'-'F', print out an appropriate message and exit. In order to compare each character of the input String to a char, use ==, and be sure to use single quotes rather than double quotes.
This is what I have so far
public static void main(String[] args) { Scanner sc = new Scanner(System.in); String hexadecimal; int decimal; int total=1; System.out.println("Enter a four digit HexaDecimal number:"); hexadecimal = sc.next(); if (hexadecimal.length() < 4) { System.out.println("That HexaDecimal number does not have four digits"); System.exit(0); } if (hexadecimal.charAt(0)=='A'){ total=10*4096; } if (hexadecimal.charAt(0)=='B'){ total=11*4096; } if (hexadecimal.charAt(0)=='C'){ total=12*4096; } if (hexadecimal.charAt(0)=='D'){ total=13*4096; } if (hexadecimal.charAt(0)=='E'){ total=14*4096; } if (hexadecimal.charAt(0)=='F'){ total=15*4096; } if (hexadecimal.charAt(0)=='A'){ total=total+10*256; } if (hexadecimal.charAt(0)=='B'){ total=total+11*256; } if (hexadecimal.charAt(0)=='C'){ total=total+12*256; } if (hexadecimal.charAt(0)=='D'){ total=total+13*256; } if (hexadecimal.charAt(0)=='E'){ total=total+14*256; } if (hexadecimal.charAt(0)=='F'){ total=total+15*256; }
if (hexadecimal.charAt(0)=='A'){ total=total+10*16; } if (hexadecimal.charAt(0)=='B'){ total=total+11*16; } if (hexadecimal.charAt(0)=='C'){ total=total+12*16; } if (hexadecimal.charAt(0)=='D'){ total=total+13*16; } if (hexadecimal.charAt(0)=='E'){ total=total+14*16; } if (hexadecimal.charAt(0)=='F'){ total=total+15*16; }
if (hexadecimal.charAt(0)=='A'){ total=total+10*1; } if (hexadecimal.charAt(0)=='B'){ total=total+11*1; } if (hexadecimal.charAt(0)=='C'){ total=total+12*1; } if (hexadecimal.charAt(0)=='D'){ total=total+13*1; } if (hexadecimal.charAt(0)=='E'){ total=total+14*1; } if (hexadecimal.charAt(0)=='F'){ total=total+15*1; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started