It looks like you're missing an else. It's not really "chopping" off the first integer, but it may seem that way since sum += 1 will be called when it shouldn't -- e.g., turning a 4 into a 5. if (next == 'V') { sum += 4; i++; } else...

java,string,algorithm,roman-numerals

My take - works with the admittedly small tests you supplied. static int rom2int(String s) { if (s == null || s.length() == 0) { return 0; } // Total value. int total = 0; // The most recent. char current = s.charAt(0); // Total for the current run. int...

arrays,ruby,hash,roman-numerals

The idea is to create a tree where every node have two childrens: the left one is what to substract and the right one is what to add. Once you get a string, you find the most valued letter and make it the first node like this: XLIX L /...

You can check the previous digit. For example, I added the condition that detects IV : if (Roman2[i]=='I'){ Roman3[i]=1; } else if (Roman2[i]=='V'){ Roman3[i]=5; if (i>0 && Roman2[i-1]=='I') { // check for IV Roman3[i]=4; Roman3[i-1]=0; } } else if (Roman2[i]=='X'){ Roman3[i]=10; } else if (Roman2[i]=='L'){ Roman3[i]=50; } else if (Roman2[i]=='C'){...

php,function,foreach,integer,roman-numerals

Yes it has something to do with the intval. You're basically casting your roman input into an integer rendering it into 0. Remove that: function romanToInteger($key) { $romans = [ 'M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400, 'C' => 100, 'XC' => 90, 'L' =>...

java,calculator,roman-numerals

So for what it is worth, Roman Numerals could not represent Zero or negative numbers, but the following edits should let your program do it. Where you have: if (input <1 || input < 999) System.out.println("negative roman numeral value "); Use: if (input < 0){ s="-"; input *= -1; }...

But Roman numerals would be strings ("III", "IV")… In general, you are correct that Roman numbers tend to be strings, not chars. However, for certain specific Roman numerals — Ⅰ, Ⅱ, Ⅲ, Ⅳ, …, Ⅻ, Ⅼ, Ⅽ, Ⅾ, Ⅿ, as well as their lower-case versions — Unicode has dedicated...

romanNumeral characterAtIndex:c+1 -- this would seem to to be out of bounds on the final loop iteration. You getting an NSRangeException?...