Write a program to convert a 4 digit hexadecimal number into decimal. The user s
ID: 3888713 • Letter: W
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 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;
}
Explanation / Answer
Note: I developed a new program from the scratch...which will meet your requirement according to the description.If u need further modification.Just give me message in comment section..Thank You.
___________________
HexaDecimalToDecimal.java
import java.util.Scanner;
public class HexaDecimalToDecimal {
public static void main(String[] args) {
// Declaring variables
String hexa;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
// Getting the input entered by the user
System.out.print("Enter String of 4 characters :");
hexa = sc.next();
// If the string has less than 4 characters then display error message
if (hexa.length() < 4) {
System.out
.println("** Invalid Input.String must be 4 characters **");
System.exit(0);
}
// Calling the method by passing the user entered number
int decimal = hexaDecimalToDecimal(hexa);
// Displaying the output
System.out.println("The Decimal for Hexadecimal " + hexa + " is :" + decimal);
}
// This method will convert the Hexadecimal to Decimal
private static int hexaDecimalToDecimal(String hexa) {
String dig = "0123456789ABCDEF";
String upperHexa = hexa.toUpperCase();
int tot = 0, index = 0;
// This for loop will calculate the Hexadecimal to decimal number
for (int i = 0; i < hexa.length(); i++) {
for (int j = 0; j < dig.length(); j++) {
if (hexa.charAt(i) == dig.charAt(j)) {
index = j;
break;
}
}
tot = tot * 16 + (index);
}
return tot;
}
}
______________________
Output#1:
Enter String of 4 characters :1A30
The Decimal for Hexadecimal 1A30 is :6704
______________________
Output#2:
Enter String of 4 characters :1A3
** Invalid Input.String must be 4 characters **
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.