Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package numberconversions;
- import java.util.Scanner; //Handles user input
- public class NumberConversions {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- //displays choices uses can choose to convert from
- System.out.printf("1. Binary%n"
- + "2. Octal%n"
- + "3. Decimal%n"
- + "4. Hexadecimal%n");
- //asks user to enter type of number they'd like to convert
- System.out.printf("Hello! Please select what type of number you'd like to convert: ");
- String choice = scanner.next();
- int bin = Integer.parseInt(choice, 2);
- int oct = Integer.parseInt(choice, 8);
- int dec = Integer.parseInt(choice, 10);
- int hex = Integer.parseInt(choice, 16);
- //converts string choice to an integer
- int choicei = Integer.parseInt(choice);
- //asks user to enter number to be converted
- System.out.printf("Thank you. Now please enter the number you'd like to convert: ");
- int number = scanner.nextInt();
- if (choicei == 1) {
- System.out.printf("%d converted is: %n"
- + "%d Octal%n"
- + "%d Decimal%n"
- + "%d Hexadecimal", number, oct, dec, hex);
- }
- else if (choicei == 2) {
- System.out.printf("%d converted is: %n"
- + "%d Binary%n"
- + "%d Decimal%n"
- + "%d Hex", number, bin, dec, hex);
- }
- else if (choicei == 3) {
- System.out.printf("%d converted is: %n"
- + "%d Binary%n"
- + "%d Octal%n"
- + "%d Hex", number, bin, oct, hex);
- }
- else if (choicei == 4) {
- System.out.printf("%d converted is: %n"
- + "%d Binary%n"
- + "%d Octal%n"
- + "%d Decimal", number, bin, oct, dec);
- }
- else {
- System.out.printf("Sorry. Something went wrong.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement