Advertisement
brilliant_moves

Roman2Decimal.java

Nov 1st, 2012
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.92 KB | None | 0 0
  1. import java.util.Scanner;   // for user input
  2.  
  3. public class Roman2Decimal {
  4.  
  5.     /**
  6.     *   Program:    Roman2Decimal.java
  7.     *   Purpose:    Convert Roman numerals to decimal numbers
  8.     *   Creator:    Chris Clarke
  9.     *   Created:    01.11.2012
  10.     */
  11.  
  12.     public static void main(String[] args) {
  13.         Scanner scan = new Scanner(System.in);
  14.         String roman;
  15.  
  16.         if (args.length > 0)
  17.             roman = args[0];
  18.         else {
  19.             System.out.print("Enter Roman numerals for conversion: ");
  20.             roman = scan.nextLine();
  21.         }
  22.  
  23.         System.out.println("In decimal: " + convertNumeralToNumber( roman));
  24.  
  25.     } // end main
  26.  
  27.     private static int convertNumeralToNumber (String numeral) {
  28.     // read the string from left to right
  29.  
  30.         int number = 0;
  31.         int first = 0, second = 0;
  32.         numeral = numeral.toUpperCase();
  33.  
  34.         // while the string isn't empty
  35.         while (!numeral.equals("")) {
  36.             // first numeral
  37.             first = getNumber( numeral.substring(0, 1));
  38.             if (numeral.length() > 1)
  39.                 // second numeral
  40.                 second = getNumber( numeral.substring(1, 2));
  41.             else
  42.                 second = 0;
  43.  
  44.             // if first numeral is less than second numeral
  45.             if (first < second) {
  46.                 // subtract first from second, add result to number
  47.                 number += (second - first);
  48.                 // remove first two numerals from string
  49.                 numeral = numeral.substring(2, numeral.length());
  50.             } else {
  51.                 // add value of first numeral
  52.                 number += first;
  53.                 // remove first numeral from string
  54.                 numeral = numeral.substring(1, numeral.length());
  55.             }
  56.         } // end while
  57.  
  58.         return number;
  59.  
  60.     } // end convertNumeralToNumber
  61.  
  62.     public static int getNumber(String letter) {
  63.         switch (letter) {
  64.             case "I": return 1;
  65.             case "V": return 5;
  66.             case "X": return 10;
  67.             case "L": return 50;
  68.             case "C": return 100;
  69.             case "D": return 500;
  70.             case "M": return 1000;
  71.             default : System.out.println("Error: wrong Roman numeral!");
  72.                   System.exit(1);
  73.         } //end switch
  74.  
  75.         return -1;
  76.  
  77.     } // end getNumber
  78.  
  79. } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement