Skip to content

Instantly share code, notes, and snippets.

@averj
Last active January 22, 2024 00:47
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save averj/9197588 to your computer and use it in GitHub Desktop.
Save averj/9197588 to your computer and use it in GitHub Desktop.
Roman Numeral Converter

#Roman Numeral Converter#

###Usage### Using this class just requires an instance and calling of a function.

Roman->Numerical

Converter converter = new Converter();
System.out.println(converter.toNumerical("MDLXXXIV")); //1584

Numerical->Roman

Converter converter = new Converter();
System.out.println(converter.toRoman(1584)); //MDLXXXIV

isNumber() Used to check if a String is a number, helpful for validating user input.

int input = 10;
Converter converter = new Converter();
if(converter.isInt(input)) {
    System.out.println(converter.toRoman(input));
}else{
    System.out.println(converter.toNumerical(input));
}
package me.javersano.cs;
public class Converter {
public final int[] NUMBER_VALUES = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; // array containing all of the values
public final String[] NUMBER_LETTERS = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" }; // array containing all of the numerals
/**
* Method used to convert a string to a integer
* @param roman roman numeral to be converted
* @return integer
*/
public int toNumerical(String roman) {
for (int i : NUMBER_LETTERS) { // Loop through all the letters
if(roman.startsWith(NUMBER_LETTERS[i])) // Check if the string starts with that letter
return NUMBER_VALUES[i] + toNumerical(roman.replaceFirst(NUMBER_LETTERS[i], "")); // Rinse and repeats until the string is empty and return it
}
return 0; // If something went wrong, simply return 0
}
/**
* Method used to convert a integer to a roman numeral
* @param num number to be converted
* @return roman numeral
*/
public String toRoman(int num) {
String roman = ""; // Declare a string to hold the numerals
for (int i : NUMBER_VALUES) { // loop through all the values
while (num >= NUMBER_VALUES[i]) { // Check if the number is greater than the current value
roman += NUMBER_LETTERS[i]; // Add the letter to the String
num -= NUMBER_VALUES[i]; // Subtract the amount from the value
}
}
return roman; // Return the String
}
/**
* Method used to check if a string is an integer
* @param s string to be parsed
* @return boolean
*/
public boolean isInt(String s) {
try {
Integer.parseInt(s);
}catch(NumberFormatException e) {
return false;
}
return true;
}
}
@rmssoares
Copy link

Just a quick comment! In your converter.java, you don't want a for each in line 28, but instead a normal for loop. After all, the index is important, and it's what you want to use inside the loop.

@pasqLisena
Copy link

+1 @thyriki

@AlexGreeen
Copy link

check line 14. int i : number_letters. Int values in array of strings?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment