Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. package numberconversions;
  2.  
  3. import java.util.Scanner; //Handles user input
  4.  
  5. public class NumberConversions {
  6.  
  7. public static void main(String[] args) {
  8.  
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. //displays choices uses can choose to convert from
  12. System.out.printf("1. Binary%n"
  13. + "2. Octal%n"
  14. + "3. Decimal%n"
  15. + "4. Hexadecimal%n");
  16.  
  17. //asks user to enter type of number they'd like to convert
  18. System.out.printf("Hello! Please select what type of number you'd like to convert: ");
  19. String choice = scanner.next();
  20.  
  21. int bin = Integer.parseInt(choice, 2);
  22. int oct = Integer.parseInt(choice, 8);
  23. int dec = Integer.parseInt(choice, 10);
  24. int hex = Integer.parseInt(choice, 16);
  25.  
  26. //converts string choice to an integer
  27. int choicei = Integer.parseInt(choice);
  28.  
  29. //asks user to enter number to be converted
  30. System.out.printf("Thank you. Now please enter the number you'd like to convert: ");
  31. int number = scanner.nextInt();
  32.  
  33. if (choicei == 1) {
  34.  
  35. System.out.printf("%d converted is: %n"
  36. + "%d Octal%n"
  37. + "%d Decimal%n"
  38. + "%d Hexadecimal", number, oct, dec, hex);
  39. }
  40.  
  41. else if (choicei == 2) {
  42. System.out.printf("%d converted is: %n"
  43. + "%d Binary%n"
  44. + "%d Decimal%n"
  45. + "%d Hex", number, bin, dec, hex);
  46. }
  47. else if (choicei == 3) {
  48. System.out.printf("%d converted is: %n"
  49. + "%d Binary%n"
  50. + "%d Octal%n"
  51. + "%d Hex", number, bin, oct, hex);
  52. }
  53. else if (choicei == 4) {
  54. System.out.printf("%d converted is: %n"
  55. + "%d Binary%n"
  56. + "%d Octal%n"
  57. + "%d Decimal", number, bin, oct, dec);
  58. }
  59. else {
  60. System.out.printf("Sorry. Something went wrong.");
  61. }
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement