Results 1 to 3 of 3
Thread: Roman Numerals help
- 05-21-2012, 10:09 PM #1
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
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; } }
- 05-21-2012, 10:15 PM #2
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0
Re: Roman Numerals help
- 05-22-2012, 01:02 AM #3
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Convert the number to Roman numerals
By ŖàΫ ỏƒ Ңόρę in forum New To JavaReplies: 9Last Post: 11-29-2012, 10:44 PM -
Arabic to roman and Roman to Arabic Converter Program
By tabako in forum New To JavaReplies: 11Last Post: 11-02-2010, 05:46 PM -
Convert roman numerals
By matzahboy in forum New To JavaReplies: 4Last Post: 02-21-2010, 10:06 PM -
convert from roman numerals to integers and vice versa
By number1cynic in forum New To JavaReplies: 10Last Post: 01-18-2010, 11:54 PM -
Convert roman numerals to integers
By Felissa in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 11:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks