Skip to content

Instantly share code, notes, and snippets.

@potatoqualitee
Created December 31, 2015 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save potatoqualitee/9474d62e0cbb22b76a42 to your computer and use it in GitHub Desktop.
Save potatoqualitee/9474d62e0cbb22b76a42 to your computer and use it in GitHub Desktop.
Java final
/**
Convert to Roman Numerals
Author: Chrissy LeMaire
Cohort: M0706
FINAL PROJECT
Last Changed: March 10, 2007.
*/
import java.*;
import java.util.*;
import java.lang.Math.*;
public class convertRoman
{
public static void main(String args[])
{
String romanInput;
Integer runningTotal = 0,index,nextNum,currNum;
Character tokenizedInput;
/* We will use a hashmap to store the Roman Numerals and their buddies.
http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashMap.html
Hashmaps are like VB's Dictionary object. They map a key to a value.
In this case, "C" would be a key and 100 would be the value. */
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
// ^Stronger typing prevents compile warning: convertRoman.java uses unchecked or unsafe operations.
// More Info @ http://groups.google.com/group/uw.cs.cs241/msg/2fa36118400f83d1
map.put('I',1);
map.put('V',5);
map.put('X',10);
map.put('L',50);
map.put('C',100);
map.put('D',500);
map.put('M',1000);
Scanner keyboard = new Scanner(System.in);
System.out.println("Type in number to convert:");
System.out.println();
romanInput = keyboard.nextLine().toUpperCase(); // because Java is case-sensitive
romanInput = romanInput.trim(); // just in case
for (index = 0; index < romanInput.length(); index++)
{
/* Below we will check to see if there are more than 4 characters of the same type in a row.*/
if (index+3 < romanInput.length()) // In order to not throw an exeption
if (
romanInput.charAt(index) == romanInput.charAt(index+1) &&
romanInput.charAt(index) == romanInput.charAt(index+2) &&
romanInput.charAt(index) == romanInput.charAt(index+3))
{
System.out.println("Warning: As a general guideline, you may want to consider not using the same character four times in a row.");
System.out.println("");
}
/* stringTokenizer is good for full words, but not characters. We will use a charAt index instead.
More info here: http://www.ling.ohio-state.edu/~kcohen/javaStringHandling.html */
tokenizedInput = romanInput.charAt(index);
/* If the input contains anything other than IVXLCDM, Reject */
if (map.containsKey(tokenizedInput) == false)
{
System.out.println("Foo, that aint no roman numeral.");
System.exit(0);
}
/* Alright, Let's Start */
currNum = map.get(tokenizedInput); //Convert tokenziedInput (such as "I") to its numerical value ("1")
/*
We need to follow a few rules here:
1. Subtract only powers of ten, such as I, X, or C. Writing VL for 45 is not allowed: write XLV instead.
2. Subtract only a single letter from a single numeral. Write VIII for 8, not IIX; 19 is XIX, not IXX.
3. Don't subtract a letter from another letter more than ten times greater. This means that you can only
subtract I from V or X, and X from L or C, so MIM is illegal.
In this code, we will "look ahead" to the next roman numeral to determine if we must subtract or add.
We will look ahead by using index+1, but only if index+1 isn't too big of a number.
*/
if (index+1 < romanInput.length()) // To make sure we try to look past the last numeral.
{
nextNum = map.get(romanInput.charAt(index+1)); //Get the next numeral
if(currNum >= nextNum) // if its more than or equal to, add it.
runningTotal = runningTotal + currNum;
else //otherwise, subtract it. But only if it follows the rules.
if ( ((Math.log(index)/Math.log(10))%1 == 0 && index>1) || (nextNum/10>currNum) ) //this addresses rule #1 and #3
{
System.out.println("Your roman numeral is not valid.");
System.exit(0);
}
else
{
runningTotal = runningTotal + (nextNum-currNum);
index++; // This, by nature, takes care of rule #2 -- we never parse more than 2 at a time.
}
}
else
// It's the last character. It can only be added.
runningTotal = runningTotal + currNum;
}
System.out.println(runningTotal);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment