Advertisement
Guest User

[Java] Arabic to Roman Method

a guest
Nov 9th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class arabicToRoman {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner input = new Scanner(System.in);
  8.         System.out.println("Enter a number between 1 and 3999 (-1 to quit): ");
  9.  
  10.         while (!input.hasNext("-1")) {
  11.             String a = input.next();
  12.  
  13.             try {
  14.                 Integer number = Integer.parseInt(a);
  15.  
  16.                 if ((number <= 3999) && (number > 0)) {
  17.                     System.out.println(arabicToRoman(number));
  18.  
  19.                 } else if (number > 3999) {
  20.                     System.out.println("Error: number must be between 1 and 3999");
  21.  
  22.                 } else if (number == 0) {
  23.                     System.out.println("Error: The Romans did not have a way to represent negative numbers or zero.");
  24.  
  25.                 } else {
  26.                     System.out.println("Error: The Romans did not have a way to represent negative numbers or zero.");
  27.  
  28.                 }
  29.             } catch (NumberFormatException e) {
  30.                 System.out.println("You did not enter a number!");
  31.             }
  32.         }
  33.     }
  34.  
  35.     public static String arabicToRoman(int arabic) {
  36.         String a = "";
  37.  
  38.         while (arabic >= 1000) {
  39.             a += "M";
  40.             arabic -= 1000;
  41.         }
  42.         while (arabic >= 900) {
  43.             a += "CM";
  44.             arabic -= 900;
  45.         }
  46.         while (arabic >= 500) {
  47.             a += "D";
  48.             arabic -= 500;
  49.         }
  50.         while (arabic >= 400) {
  51.             a += "CD";
  52.             arabic -= 400;
  53.         }
  54.         while (arabic >= 100) {
  55.             a += "C";
  56.             arabic -= 100;
  57.         }
  58.         while (arabic >= 90) {
  59.             a += "XC";
  60.             arabic -= 90;
  61.         }
  62.         while (arabic >= 50) {
  63.             a += "L";
  64.             arabic -= 50;
  65.         }
  66.         while (arabic >= 40) {
  67.             a += "XL";
  68.             arabic -= 40;
  69.         }
  70.         while (arabic >= 10) {
  71.             a += "X";
  72.             arabic -= 10;
  73.         }
  74.         while (arabic >= 9) {
  75.             a += "IX";
  76.             arabic -= 10;
  77.         }
  78.         while (arabic >= 5) {
  79.             a += "V";
  80.             arabic -= 5;
  81.         }
  82.         while (arabic >= 4) {
  83.             a += "IV";
  84.             arabic -= 4;
  85.         }
  86.         while (arabic >= 1) {
  87.             a += "I";
  88.             arabic -= 1;
  89.         }
  90.         return a;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement