Results 1 to 3 of 3
  1. #1
    umaza is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Roman Numerals help

    I have to convert Roman Numerals to a Decimal Number, I got how to do it, but don't know to restrict somethings,

    There is a rule that one symbol cannot repeat more than three times at once, so XIIII should not work, instead it should be XIV, but I do not know how to do this.

    I put an if statement within the for loop in class Translate, method numeral, but it does not work.


    Java Code:
    import java.io.*;
    import java.util.*;
    
    
    public class RomanNumeral
    {
      public static void main (String[] args) throws IOException
      {  
        BufferedReader objReader = new BufferedReader (new InputStreamReader (System.in));
        
        
        for (;;) 
        {
          System.out.println();
          System.out.println("Enter roman numeral numbers:");
          String romanNumeral = objReader.readLine();
          
          Translate arabNumeral = new Translate(romanNumeral);
          
          String finalOut = arabNumeral.getNumeral();
          
          System.out.println("The conversion is:");
          System.out.println(finalOut);  
        }
        
        
      } 
    }
    
    class Translate
    {
      private String word, roman, result;
      private int length, addition = 0;
      
      
      Translate(String roman)
      {
        this.roman = roman;
        numeral();
      }
      
      public void numeral()
      {
        Scanner conversion = new Scanner(roman);
        StringBuffer combine = new StringBuffer ("");
        
        
        while (conversion.hasNext())
        {
          word = conversion.next();
          length = word.length(); 
          
          
          for (int i = 0; i < length-1;i++)
          {
              if (word.charAt(i) == 'I' && (word.charAt(i+1) == 'V' || word.charAt(i+1) == 'X' || word.charAt(i+1) == 'L' || word.charAt(i+1) == 'C' || word.charAt(i+1) == 'D' || word.charAt(i+1) == 'M'))
              {
                addition--;
              }
              else if (word.charAt(i) == 'I' && word.charAt(i+1) == 'I')
              {
                addition++;
              }
              else if (word.charAt(i) == 'V' && (word.charAt(i+1) == 'X' || word.charAt(i+1) == 'L' || word.charAt(i+1) == 'C' || word.charAt(i+1) == 'D' || word.charAt(i+1) == 'M'))
              {
                addition -= 5;
              }
              else if (word.charAt(i) == 'V' && (word.charAt(i+1) == 'I' || word.charAt(i+1) == 'V'))
              {
                addition += 5;
              }
              else if (word.charAt(i) == 'X' && (word.charAt(i+1) == 'L' || word.charAt(i+1) == 'C' || word.charAt(i+1) == 'D' || word.charAt(i+1) == 'M'))
              {
                addition -= 10;
              }
              else if (word.charAt(i) == 'X' && (word.charAt(i+1) == 'I' || word.charAt(i+1) == 'V' || word.charAt(i+1) == 'X'))
              {
                addition += 10;
              }
              else if (word.charAt(i) == 'L' && (word.charAt(i+1) == 'C' || word.charAt(i+1) == 'D' || word.charAt(i+1) == 'M'))
              {
                addition -= 50;
              }
              else if (word.charAt(i) == 'L' && (word.charAt(i+1) == 'I' || word.charAt(i+1) == 'V' || word.charAt(i+1) == 'X' || word.charAt(i+1) == 'L'))
              {
                addition += 50;
              }
              else if (word.charAt(i) == 'C' && (word.charAt(i+1) == 'D' || word.charAt(i+1) == 'M'))
              {
                addition -= 100;
              }
              else if (word.charAt(i) == 'C' && (word.charAt(i+1) == 'I' || word.charAt(i+1) == 'V' || word.charAt(i+1) == 'X' || word.charAt(i+1) == 'L' || word.charAt(i+1) == 'C'))
              {
                addition += 100;
              }
              else if (word.charAt(i) == 'D' && (word.charAt(i+1) == 'M'))
              {
                addition -= 500;
              }
              else if (word.charAt(i) == 'D' && (word.charAt(i+1) == 'I' || word.charAt(i+1) == 'V' || word.charAt(i+1) == 'X' || word.charAt(i+1) == 'L' || word.charAt(i+1) == 'C' || word.charAt(i+1) == 'D'))
              {
                addition += 500;
              }
              else if (word.charAt(i) == 'M' && (word.charAt(i+1) == 'I' || word.charAt(i+1) == 'V' || word.charAt(i+1) == 'X' || word.charAt(i+1) == 'L' || word.charAt(i+1) == 'C' || word.charAt(i+1) == 'D' || word.charAt(i+1) == 'M'))
              {
                addition += 1000;
              }
          }
          if (word.charAt(length-1) == 'I')
          {
            addition++; 
          }
          else if (word.charAt(length-1) == 'V')
          {
            addition += 5; 
          }
          else if (word.charAt(length-1) == 'X')
          {
            addition += 10; 
          }
          else if (word.charAt(length-1) == 'L')
          {
            addition += 50; 
          }
          else if (word.charAt(length-1) == 'C')
          {
            addition += 100; 
          }
          else if (word.charAt(length-1) == 'D')
          {
            addition += 500; 
          }
          else if (word.charAt(length-1) == 'M')
          {
            addition += 1000; 
          }
          
          combine.append(addition);
          combine.append(' ');
          
          addition = 0;
        }
        result = combine.toString ();
      }
      
      public String getNumeral()
      {
        return result;
      }  
    }

  2. #2
    kackin is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

  3. #3
    umaza is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Convert the number to Roman numerals
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 9
    Last Post: 11-29-2012, 11:44 PM
  2. Replies: 11
    Last Post: 11-02-2010, 06:46 PM
  3. Convert roman numerals
    By matzahboy in forum New To Java
    Replies: 4
    Last Post: 02-21-2010, 11:06 PM
  4. convert from roman numerals to integers and vice versa
    By number1cynic in forum New To Java
    Replies: 10
    Last Post: 01-19-2010, 12:54 AM
  5. Convert roman numerals to integers
    By Felissa in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 12:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •