Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Rosetta PGA-TRAM

by whakka (Hermit)
on Jun 16, 2009 at 17:06 UTC ( [id://772078]=note: print w/replies, xml ) Need Help??


in reply to Rosetta PGA-TRAM

Pretty straightforward to translate to a Java version:
import java.util.HashMap; public class pgaTram { private static HashMap<Character, Integer> rtoa = loadRTOA(); public static void main(String[] args) { String[] testdata = { "XLII", "LXIX", "mi" }; for (String t : testdata) { System.out.println(t + ": " + romanToDec(t)); } } public static int romanToDec(String s) { int t = 0; for (char c : s.toCharArray()) { int n = rtoa.get(Character.toUpperCase(c)); t += n - t % n * 2; } return t; } private static HashMap<Character, Integer> loadRTOA() { HashMap<Character, Integer> rtoa = new HashMap<Character, Inte +ger>(); rtoa.put('M', 1000); rtoa.put('D', 500); rtoa.put('C', 100); rtoa.put('L', 50); rtoa.put('X', 10); rtoa.put('V', 5); rtoa.put('I', 1); return rtoa; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://772078]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-20 03:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found