Hello I can\'t figure out how to write this code can somebody help me? Write a p
ID: 3909063 • Letter: H
Question
Hello I can't figure out how to write this code can somebody help me?
Write a program that asks the user to enter a number. Then ask the user to enter a “from” and “to” radix (also known as base). So for example, if the user enters AA and the from radix is 16 and the to radix is 10, then the answer is 170.
You may want to check out the Integer class and the parseInt and toString functions. You can also use loops.
It is very important that your program doesn’t crash. try/catch – from chapter 11
For the starting number, keep prompting them until they enter something.
For the from and to radixes, keep prompting the user for input, until they enter an integer.
Sample console session below:
Enter a number to convert> G
Enter the base to convert from (bases are atleast 2)> 16
It has to be big enough for G
Enter the base to convert from (bases are atleast 2)> X
It has to be an integer
Enter the base to convert from (bases are atleast 2)> 1.2
It has to be an integer
Enter the base to convert from (bases are atleast 2)> 20
Enter the base to convert to (bases are atleast 2)> Y
It has to be an integer
Enter the base to convert to (bases are atleast 2)> 1
Enter the base to convert to (bases are atleast 2)> 2
10000
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
import java.util.Scanner;
public class Convert {
private static Scanner keyboard = new Scanner(System.in);
private static int inputBase(String msg){
int base = 0;
do{
System.out.print(msg);
String input = keyboard.next();
try{
base = Integer.parseInt(input);
}catch(NumberFormatException e){
System.out.println("It has to be an integer");
continue;
}
}while(base < 2); //valid base
return base;
}
private static boolean isValidInBase(String number, int base){
try{
Integer.parseInt(number, base); // try to parse the number in given base
return true;
}catch(NumberFormatException e){ //exceptoion is thrown if invalid
return false;
}
}
public static void main(String[] args) {
int from, to;
String number;
System.out.print("Enter a number to convert> ");
number = keyboard.next();
while(true)
{
from = inputBase("Enter the base to convert from (bases are atleast 2)> ");
if(!isValidInBase(number, from))
System.out.println("It has to be big enough for " + number);
else
break;
}
to = inputBase("Enter the base to convert to (bases are atleast 2)> ");
int decimal = Integer.parseInt(number, from);
String answer = Integer.toUnsignedString(decimal, to);
System.out.println("In base " + to + ", the number is " + answer);
}
}
output
-----
Enter a number to convert> G
Enter the base to convert from (bases are atleast 2)> 16
It has to be big enough for G
Enter the base to convert from (bases are atleast 2)> X
It has to be an integer
Enter the base to convert from (bases are atleast 2)> 1.2
It has to be an integer
Enter the base to convert from (bases are atleast 2)> 20
Enter the base to convert to (bases are atleast 2)> Y
It has to be an integer
Enter the base to convert to (bases are atleast 2)> 1
Enter the base to convert to (bases are atleast 2)> 2
In base 2, the number is 10000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.