Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Roman Numeral converter

807598Oct 17 2006 — edited Oct 18 2006
Hey, I've received an error message in my roman numeral program and am not what I'm doing wrong.

Here's the error:


C:\Documents and Settings\Mitch Thul\Desktop\Java2\lab5\Roman2.java:22: cannot find symbol
symbol : variable length
location: class java.lang.String
for(int i = 0; i <= roman.length - 2; i++)
^
C:\Documents and Settings\Mitch Thul\Desktop\Java2\lab5\Roman2.java:25: cannot find symbol
symbol : method charAt(int)
location: class Roman2
if((charAt(i)) >= (charAt(i+1)))
^
C:\Documents and Settings\Mitch Thul\Desktop\Java2\lab5\Roman2.java:25: cannot find symbol
symbol : method charAt(int)
location: class Roman2
if((charAt(i)) >= (charAt(i+1)))
^
3 errors

Tool completed with exit code 1


And here's my code:
//Mitch Thul 3059899

import java.util.Scanner;
import java.lang.String;

public class Roman2
{
	public static void main(String[] arg)
	{
		Scanner keyboard = new Scanner(System.in);
		String date;
		System.out.println("Please enter a roman numeral date:");
		date = keyboard.next();
		System.out.println("Roman numeral = " + date);
	}
		public static int romanToInt(String roman)
		{
			//takes a string of roman numerals (date) and produces corresponding int
			int n; //value of next numeral
			roman = roman + "0"; //adds 0 to the end of the numeral string (for simplification)
			int total = 0; //start at position 0
			for(int i = 0; i <= roman.length - 2; i++)
			{
				n = numeralToInt(roman.charAt(i));
				if((charAt(i)) >= (charAt(i+1)))
				{
					total += n; // add n to the total
				}
				else
				{
					total -= n; //subtract n from the total
				}
				return total;
			}
		}
		private static int numeralToInt(char ch)
		{
			switch(ch)
			{
				case 'M': return 1000;
				case 'D': return 500;
				case 'C': return 100;
				case 'L': return 50;
				case 'X': return 10;
				case 'V': return 5;
				case 'I': return 1;
				case '0': return 0;
			}
			return 0;
		}
}
Thanks in advance for any help.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 15 2006
Added on Oct 17 2006
13 comments
363 views