Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Rosetta PGA-TRAM

by whakka (Hermit)
on Jun 16, 2009 at 17:06 UTC ( #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; } }


Comment on Re: Rosetta PGA-TRAM
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2014-08-01 15:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (27 votes), past polls