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.