fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public enum RomanDigit {
  11. i(1, true), v(5, false), x(10, true), l(50, false), c(100, true), d(500, false), m(1000, true);
  12.  
  13. private int value;
  14. private boolean repeatable;
  15.  
  16. RomanDigit (int value, boolean repeatable){
  17. this.value = value;
  18. this.repeatable = repeatable;
  19. }
  20.  
  21. public int getValue() {
  22. return value;
  23. }
  24.  
  25. public boolean isRepeatable() {
  26. return repeatable;
  27. }
  28.  
  29. public boolean subtractableFrom(RomanDigit other) {
  30. // Currently subtractale digits happen to be the same ones that are repeatable.
  31. // Should this change, then another solution is needed.
  32. if (other == null || !this.isRepeatable()) {
  33. return false;
  34. }
  35.  
  36. int oridinal = this.ordinal();
  37. int otherOridinal = other.ordinal();
  38.  
  39. return (oridinal == otherOridinal - 1 || oridinal == otherOridinal - 2);
  40. }
  41. }
  42.  
  43. public static int convertRomanNumber(String romanNumber) {
  44. RomanDigit previousDigit = null;
  45. int characterRepeatCount = 1;
  46. int total = 0;
  47.  
  48. for(int i = 0; i < romanNumber.length(); i++){
  49. // Also throws IllegalArgumentException if a digit is invalid
  50. RomanDigit currentDigit = RomanDigit.valueOf(String.valueOf(romanNumber.charAt(i)));
  51. int currentRomanCharNumericValue = currentDigit.getValue();
  52.  
  53. if (currentDigit.equals(previousDigit)) {
  54. characterRepeatCount++;
  55.  
  56. if (characterRepeatCount > 3) {
  57. throw new IllegalArgumentException("Repeatable Digit is repeated too often"); // Error message needs more details
  58. }
  59. if (currentDigit.isRepeatable()) {
  60. total += currentRomanCharNumericValue;
  61. } else {
  62. throw new IllegalArgumentException("Unrepeatable Digit is repeated"); // Error message needs more details
  63. }
  64. } else if (previousDigit != null && previousDigit.compareTo(currentDigit) < 0) {
  65. if (characterRepeatCount > 1) {
  66. throw new IllegalArgumentException("Repeatable Digit is repeated before larger digit"); // Error message needs more details
  67. }
  68. if (previousDigit.subtractableFrom(currentDigit)) {
  69. characterRepeatCount = 1;
  70. total += currentRomanCharNumericValue - (2 * previousDigit.getValue());
  71. } else {
  72. throw new IllegalArgumentException("Digit may not be subtracted from other digit"); // Error message needs more details
  73. }
  74. } else {
  75. characterRepeatCount = 1;
  76. total += currentRomanCharNumericValue;
  77. }
  78.  
  79. previousDigit = currentDigit;
  80. }
  81. return total;
  82. }
  83.  
  84.  
  85. public static void main (String[] args) throws java.lang.Exception
  86. {
  87. try {
  88. System.out.println( convertRomanNumber("ii") );
  89. System.out.println( convertRomanNumber("iii") );
  90. System.out.println( convertRomanNumber("iv") );
  91. System.out.println( convertRomanNumber("vi") );
  92. System.out.println( convertRomanNumber("ix") );
  93. System.out.println( convertRomanNumber("mcmxliv") );
  94. // TODO handle Errors better
  95. System.out.println( e.getMessage() );
  96. }
  97.  
  98. }
  99. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
2
3
4
6
9
1944