Question

NO LOOPS, write a program, in Java, that prompts the user for a hexadecimal string of...

NO LOOPS, write a program, in Java, that prompts the user for a hexadecimal string of EXACTLY 4 hex digits, no more, no less, converts the hexadecimal value to its decimal value using string, character parsing, and Math methods learned in this chapter, and then prints the original string and its converted decimal value. NOTE: Hex digits may be in UPPER or lower case.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class HexToDec {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter String with 4 digits");

String str=sc.nextLine();

while(str.length()<4){

System.out.println("Enter String with 4 digits");

str=sc.nextLine();

}

System.out.println(finDec(str,0,0,str.length()-1));

}

private static double finDec(String str,double sum,int p,int l) {

if(l<0)

return sum;

char c=str.charAt(l);

int n =0;

if(c>='0' && c<='9'){

n=c-48;

}

if(c=='a'||c=='A')

n=10;

if(c=='b'||c=='B')

n=11;

if(c=='c'||c=='C')

n=12;

if(c=='d'||c=='D')

n=13;

if(c=='e'||c=='E')

n=14;

if(c=='f'||c=='F')

n=15;

sum=sum + n *Math.pow(16, p);

return finDec(str, sum, p+1, l-1);

}

}

Add a comment
Know the answer?
Add Answer to:
NO LOOPS, write a program, in Java, that prompts the user for a hexadecimal string of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program which takes a string as a user input. Create 2 functions. The...

    Write a Java program which takes a string as a user input. Create 2 functions. The first function expects a string as argument and returns void. It converts all upper case characters to lower case and converts all lower case characters to upper case. It then prints out the converted string to a console. The second function also expects a string as argument and returns void. It will find first charactor that is repeated exactly 2 times in the string....

  • Write a program that prompts a user for an input string of length at least two...

    Write a program that prompts a user for an input string of length at least two and prints the string with the first half in upper case and the second half in lower case. If the length of the string is odd, the "second half" should be one character longer than the "first half". Show Hint

  • In Java, write a recursive method that converts an integer to hexadecimal. The function should print...

    In Java, write a recursive method that converts an integer to hexadecimal. The function should print out the hexadecimal character instead of returning the character. Write a test program that prompts the user to enter an integer and displays its hexadecimal equivalent.

  • Please help me with this program in Java: Write a Java program that prompts the user...

    Please help me with this program in Java: Write a Java program that prompts the user to enter a password that matches a specific pattern. Your program must approve the user's entry.. Here is the pattern, in this order: 1 or more upper case letters two lower case letters 1 or 2 digits zero or 1 upper case letters any two of this group @#$%^&

  • String variables/Selection & loop Write a complete Java program which prompts the user for a sentence on one line wh...

    String variables/Selection & loop Write a complete Java program which prompts the user for a sentence on one line where each word is separated by one space, reads the line into one String variable using nextline(), converts the string into Ubbi dubbi and displays the translated sentence. Ubbi dubbi works by adding ub before each vowel sound in a syllable. For example, hello becomes hubellubo. The word speak has the vowel sound ea, so in Ubbi dubbi it becomes spubeak....

  • (Process a string) Write a program that prompts the user to enter a string and displays...

    (Process a string) Write a program that prompts the user to enter a string and displays its length and its first character. java StringLength Enter a string:Does quantum determinacy have anything to with whether human cognition is deterministic? The string is of length 88 and the first character is D import java.util.Scanner; class StringLength{    public static void main (String args[]){        Scanner input = new Scanner(System.in);        System.out.println("Enter a string:");        String ans = input.nextLine();        System.out.println("The string...

  • In Java, write a program that prompts the user to input a sequence of characters and...

    In Java, write a program that prompts the user to input a sequence of characters and outputs the number of vowels. You will need to write a method to return a value called isAVowel which will return true if a given character is a vowel and returns false if the character is not a vowel. A second method, called isAConstant, should return true if a given character is a constant and return false if the character is not a constant....

  • Write a program that prompts the user for a String value and a character value. The...

    Write a program that prompts the user for a String value and a character value. The program should then find the last occurrence of the provided character in the provided String and display the corresponding index. If the character is not found in the String, display -1. For additional challenge, create a multi-class solution. The business class should consist of two instance varaibles to store the user provided values and one method to accomplish the given task. The tester class...

  • Write a Java program that prompts user to select one of the five shapes(Rectangle, Square, Triangle,...

    Write a Java program that prompts user to select one of the five shapes(Rectangle, Square, Triangle, Circle, and Parallelogram), then calculate area of the shape,must have input validation and uses more of java library like math class, wrapper class, string methods, use formatted output with the printf method. Finally, create a pseudo-code statement and flowchart

  • Using Java write a program that takes a string input from the user and then outputs...

    Using Java write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT