Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class RomanNumeral {
  4.     /*
  5.      * Description:
  6.      *
  7.      * In this program, you are to create a Roman Numeral class to handle Roman
  8.      * numeral operations. The class is supposed to convert numerical input to
  9.      * Roman numerals, and vice versa.
  10.      */
  11.     private static String[] romanNumerals = { "M", "CM", "D", "CD", "C", "XC",
  12.             "L", "XL", "X", "IX", "V", "IV", "I" };
  13.     private static int[] numbers = { 1000, 900, 500, 400, 100, 90, 50, 40, 10,
  14.             9, 5, 4, 1 };
  15.     private static String resultString = "";
  16.     private static int resultInt = 0;
  17.  
  18.     public static String numberToRomanNumeral(int input) {
  19.         String result = "";
  20.         for (int i = 0; i < numbers.length; i++) {
  21.             if (input - numbers[i] >= 0) {
  22.                 input -= numbers[i];
  23.                 result += "" + romanNumerals[i];
  24.             }
  25.         }
  26.         return result;
  27.     }
  28.  
  29.     public static int romanNumeralToNumber(String input) {
  30.         int result = 0;
  31.         for (int i = 0; i < romanNumerals.length; i++) {
  32.             while (input.startsWith(romanNumerals[i])) {
  33.                 result += numbers[i];
  34.                 input = input.substring(romanNumerals[i].length());
  35.             }
  36.         }
  37.         return result;
  38.     }
  39.    
  40.     public static void main(String[] args) {
  41.         Scanner scanner = new Scanner(System.in);
  42.         System.out
  43.                 .println("Enter 1 to have a Roman numeral converted to a number"
  44.                         + " or 2 to have a number converted into a Roman Numeral.");
  45.         String choice = scanner.nextLine();
  46.         int choiceInteger = 0;
  47.         try {
  48.             choiceInteger = Integer.parseInt(choice);
  49.         } catch (NumberFormatException e) {
  50.             System.out.println("You didn't enter a valid option.");
  51.             System.exit(1);
  52.         }
  53.  
  54.         switch (choiceInteger) {
  55.         case 1: {
  56.             System.out
  57.                     .println("Enter the Roman Numeral to be converted into an integer");
  58.             String input = scanner.nextLine().toUpperCase();
  59.             if (input.matches("[MCDXLVI]*")) {
  60.                 resultInt = RomanNumeral.romanNumeralToNumber(input);
  61.                 System.out.println(input + " is " + resultInt);
  62.                 break;
  63.             }
  64.             else
  65.                 System.out.println("You didn't enter a Roman Numeral.  Try again.");
  66.                 System.exit(1);
  67.         }
  68.         case 2: {
  69.             System.out
  70.                     .println("Enter the number to be converted into a Roman numeral");
  71.             String input = scanner.nextLine();
  72.             int inputInt = 0;
  73.             try {
  74.                 inputInt = Integer.parseInt(input);
  75.             } catch (NumberFormatException e) {
  76.                 System.out.println("You didn't enter an integer!");
  77.                 System.exit(1);
  78.             }
  79.             resultString = RomanNumeral.numberToRomanNumeral(inputInt);
  80.             System.out.println(inputInt + " is " + resultString);
  81.             break;
  82.         }
  83.         }
  84.     }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement