Page 1 of 1

Roman and Arabic Numeral Converter Convert Roman to Arabic and vice versa Rate Topic: -----

#1 remorseless  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 129
  • Joined: 08-August 09

Posted 14 December 2009 - 08:08 PM

Hey guys and gals, it's me, remorseless, back with another tutorial, this time in Java. We will be creating today a class that converts Roman to Arabic or vice versa. There's one catch though, the numbers have to be between (and including) 3999 and 1, simply because Roman characters for 4000+ are Unicode and we just aren't going to go into that.

This class is simple, it will have methods that can check whether a number is a proper Arabic or Roman number and it will have methods that return strings containing a converted Arabic or Roman number. Sound a bit complicated? Well, it's not ;) , and without further ado, let's begin.

First off, let's name this class RomanArabicConverter for simplicity. Let's begin with writing a method which checks if a number is proper Arabic or not and if that number is higher than 3999 or lower than 1 too.
//	  Start of Class - RomanArabicConverter
public class RomanArabicConverter {

		//	  Checks whether the entered Arabic numeral is valid
	public boolean isValidArabic(int x) {
		String num = String.valueOf(x);

	   //	   Checks each character if it is a digit.
		for (int k = 0; k < num.length(); k++) {
			if (Character.isDigit(num.charAt(k)) == false) {
				return false;
			}
		}

		//  Checking if the number is bigger than 3999 or smaller than 1
		if (x > 3999 ||x < 1) {
			return false;
		}
		return true;
	}



Pretty simple so far, don't you agree? B) Now onto the next part, a method which checks if a string is a proper roman number. Let's see:

	//	  Checks whether the entered Roman numeral is valid
	public boolean isValidRoman(String num) {

	//	  Checks each character if it is one of I, V, , L, C, D, M (Roman characters)
		for (int k = 0; k < num.length(); k++) {
			if (num.charAt(k) != 'I' &&
					num.charAt(k) != 'V' &&
					num.charAt(k) != 'X' &&
					num.charAt(k) != 'L' &&
					num.charAt(k) != 'C' &&
					num.charAt(k) != 'D' &&
					num.charAt(k) != 'M') {
				return false;
			}
		}
		return true;
	}



NOTE: Even though it checks if it a proper roman character, this is a pretty flawed checking simply because someone could enter IIIIIIIIIIX and it would still return true, so keep that in mind.

Now we make a method which turns a number into Roman. This is quite simple even though it looks complicated.

	 //	  Returns a string containing the entered Arabic numeral in Roman numeral form.
	public String toRoman(int num) {

		if (isValidArabic(num)) {  //	   Checking if the number is a valid Arabic number

			String Roman = ""; //	   This will be our result string.

			//	  Declare and Initiate our Arrays
			String onesArray[] = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
			String tensArray[] = {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
			String hundredsArray[] = {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};

			//	  Get the ones in the number
			int ones = num % 10;

			//	  Get the tens
			num = (num - ones) / 10;
			int tens = num % 10;

			//	  Get the hundreds
			num = (num - tens) / 10;
			int hundreds = num % 10;

			//	  Get and write the thousands in the number to our string
			num = (num - hundreds) / 10;
			for (int i = 0; i < num; i++) {
				Roman += "M";
			}

			//	  Write the hundreds
			if (hundreds >= 1) {
				Roman += hundredsArray[hundreds - 1];
			}

			//	  Write the tens
			if (tens >= 1) {
				Roman += tensArray[tens - 1];
			}

			//	  And finally, write the ones
			if (ones >= 1) {
				Roman += onesArray[ones - 1];
			}

			//	  Return our string.
			return String.valueOf(Roman);
		}else{
		return null; //		If the number isn't a valid Arabic number, return null.
		}
	}



It's pretty self explanatory, this will return a string containing the Arabic number in Roman form. But the next part is the ninja skills part. :ph34r: Turning a roman number into Arabic. Follow along:

  //	  Returns a string containing the entered Roman numeral in Arabic numeral form.
	public String toArabic(String s) {

		if (isValidRoman(s)) { //	   Check if the number is a valid Roman Number

			int Arabic = 0; //	  This will be our result.
			int last_digit = 0;
			int current_digit = 0;

			for (int i = 0; i < s.length(); i++) {

				if (s.charAt(i) == 'I') {
					current_digit = 1;
				}
				if (s.charAt(i) == 'V') {
					current_digit = 5;
				}
				if (s.charAt(i) == 'X') {
					current_digit = 10;
				}
				if (s.charAt(i) == 'L') {
					current_digit = 50;
				}
				if (s.charAt(i) == 'C') {
					current_digit = 100;
				}
				if (s.charAt(i) == 'D') {
					current_digit = 500;
				}
				if (s.charAt(i) == 'M') {
					current_digit = 1000;
				}

				//This is the tricky part.
				//If the last number is smaller than the curren number, subtract the last number from the current number
				//Otherwise, just add the current number. We must also skip the first number from this rule simply because
				//e.g. someone enters 1799 in which case it would subtract 1 from 7

				if (last_digit < current_digit && last_digit != 0) {
					current_digit -= last_digit;
					Arabic -= last_digit;
					Arabic += current_digit;
					last_digit = current_digit;
					current_digit = 0;
				} else {
					last_digit = current_digit;
					Arabic += current_digit;
					current_digit = 0;
				}
			}

			//	  Return our string.
			return String.valueOf(Arabic);

		}else{
		return null;		//	  Return null if the number entered is not a proper roman number.
		}
	}

}
//Tutorial by remorseless



And there we have it, boys and girls. A class that converts Roman to Arabic and vice versa. Go ahead, test it yourself, use it in your program, do whatever floats your boat with it ;) . Hope this was a good tutorial and that it helps you in your Java programming life. I'm sorta tired so I think I'll hit the sack. G'night. :sleepy:

Is This A Good Question/Topic? 1
  • +

Page 1 of 1