Computer Science Canada

String Manipulation - Roman Numerals

Author:  Gryphon [ Mon Jun 09, 2008 8:32 pm ]
Post subject:  String Manipulation - Roman Numerals

I am currently trying to make a program that converts Roman numerals to arabic numbers(f.y.i. arabic numbers are just normal numbers, 1,2,3 etc.) and visa versa. I think I have a general idea of what is going on but I'm fairly new to many of these string manipulation commands. The problem is that when I enter any input, it is never converted, the program simply outputs whatever the answer variable is initialized as. I think the problem may be how I'm using all the string commands. Any help with this would be greatly appreciated! Thanks! Heres my code so far:

code:

import java.io.*;
public class RomanConverter
{       public static void main(String[] args)
        {       BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
                int arabic = 0;
               
                System.out.println("1) Roman Numerals to Arabic numbers, 2) Arabic numbers to Roman Numbers");
                //get(keyboard);
                               
                if(get(keyboard).equals("1"))
                {       System.out.println("Enter the Roman numerals you would like to convert to Arabic numbers");
                        System.out.println(romanToArabic(get(keyboard)));
                }
                else
                {       System.out.println("Enter the Arabic numbers you would like to convert to Roman numerals");
                        arabic = Integer.valueOf(get(keyboard));
                        System.out.println(arabicToRoman(arabic));
                }
               
        }
       
        private static String get(BufferedReader br)
        {       try
                {       String inputString=br.readLine();
                        return inputString;
                }
                catch(IOException e)
                {       System.out.println("ERROR");
                }
                return null;
        }       
       
        private static int romanToArabic(String s)
        {       int answer = 0;
                               
                s.toUpperCase();               
                checkForReplacements(s);
               
                for(int i=0;i>s.length();i++)
                {       if(s.contains("M"))
                        {       s.replaceFirst("M"," ");
                                s.trim();
                                answer += 1000;
                        }
                        else if(s.contains("D"))
                        {       s.replaceFirst("D"," ");
                                s.trim();
                                answer += 500;
                        }
                        else if(s.contains("C"))
                        {       s.replaceFirst("C"," ");
                                s.trim();
                                answer += 100;
                        }
                        else if(s.contains("L"))
                        {       s.replaceFirst("L"," ");
                                s.trim();
                                answer += 50;
                        }
                        else if(s.contains("X"))
                        {       s.replaceFirst("X"," ");
                                s.trim();
                                answer += 10;
                        }
                        else if(s.contains("V"))
                        {       s.replaceFirst("V"," ");
                                s.trim();
                                answer += 5;
                        }
                        else if(s.contains("I"))
                        {       s.replaceFirst("I"," ");
                                s.trim();
                                answer += 1;
                        }
                }
                return answer;
        }       
       
        private static void checkForReplacements(String s)
        {       s.replaceAll("CM", "DCCCC");//900
                s.replaceAll("CD", "CCCC");//400
                s.replaceAll("XC", "LXXXX");//90
                s.replaceAll("XL", "XXXX");//40
                s.replaceAll("IX", "VIIII");//9
                s.replaceAll("IV", "IIII");//4
        }
       
        private static String arabicToRoman(int n)
        {       String answer = "";
                       
                while(n > 0)
                {       if(n >= 1000)
                        {       answer.concat("M");
                                n -= 1000;
                        }
                        else if(n >= 500)
                        {       answer.concat("D");
                                n -= 500;
                        }
                        else if(n >= 100)
                        {       answer.concat("C");
                                n -= 100;
                        }
                        else if(n >= 50)
                        {       answer.concat("L");
                                n -= 50;
                        }
                        else if(n >= 10)
                        {       answer.concat("X");
                                n -= 10;
                        }
                        else if(n >= 5)
                        {       answer.concat("V");
                                n -= 5;
                        }
                        else if(n >= 1)
                        {       answer.concat("I");
                                n -= 1;
                        }
                }
                replace(answer); //Replaces IIII with IV, and others
                return answer; 
        }
       
        private static void replace(String answer)
        {       answer.replaceAll("DCCCC", "CM");//900
                answer.replaceAll("CCCC", "CD");//400
                answer.replaceAll("LXXXX", "XC");//90
                answer.replaceAll("XXXX", "XL");//40
                answer.replaceAll("VIIII", "IX");//9
                answer.replaceAll("IIII", "IV");//4
        }
}

Author:  OneOffDriveByPoster [ Mon Jun 09, 2008 9:04 pm ]
Post subject:  Re: String Manipulation - Roman Numerals

The methods in the String class return new strings that are modified the way you would expect. They are not mutators. You can check out StringBuffer.


: