Java Beginner Needs Help Debugging Roman Numeral Converting Code!

Hello! I'm studying right now as an exchange student in Germany and taking my first programming course in Java, and in the last week, things have just started to go over my head and I'm kind of drowning while trying to get all the homework done!

The assignment was supposed to write a code to convert roman numerals into arabic ones - here's what I have written so far:

static int roman2Decimal(String number)
{
int[] numbers = { 1000, 900, 500, 400, 100, 90,
50, 40, 10, 9, 5, 4, 1 }; //it's much easier to program in the values for IV, IX, etc
String[] letters = { "M", "CM", "D", "CD", "C", "XC",
"L", "XL", "X", "IX", "V", "IV", "I" }; //than to use a bunch of 'if's and things to check if a // letter with a lower value is in front of one with a higher one.
final int num;
System.out.println("Please enter a roman numeral using the letters M, D, C, L, X, V, I. All letters will be treated as capitalized.");
System.out.println("Please do not enter a roman numeral with a value greater than 3999."); //3999 is the highest value since we're only
Scanner roman = new Scanner(System.in); //using the roman numerals up to M, and you can't have 4 in a row.
roman = roman.toUpperCase();
roman = roman.trim();
if (roman.length()=0)
{
System.out.println("Please enter an actual number using the above mentioned roman numerals, not just empty spaces.");
roman = input.nextString();
}
int i = 0; //this will be our position in the string of the roman numerals.
int decimal = 0; //this is the decimal value of the roman numerals converted up until this point.
while (i<roman.length())
{
char letter = roman.CharAt(i); //the letter at the position i in the string roman
int number = letterToNumber(letter); //The number that the letters represent - will be found later in the program.
if (number<0)
{
System.out.println("Illegal character " + letter + " in the string you entered. Please try again using ONLY M, D, C, L, X, V, and I.");
roman = input.nextString();
}
if (i == roman.length()) //There are no characters after where we are (at i), so just add the value of that letter to the number.
{
decimal += number;
}
else
{
int nextNumber = letterToNumber(roman.CharAt(i)); //move to look at the next letter in the string
if (nextNumber > number) //if the second number is greater than the one preceeding it, we need to subtract it from the original number.
{
decimal += (nextNumber - number);
i++;
}
else
{
decimal += number;
}
}
} //end while loop
if (decimal > 3999)
{
throw new NumberFormatException("The roman numeral should have a value strictly less than 4000.");
}
num = decimal;
int letterToNumber(char letter)
//This will convert the Roman Numerals to their decimal counterparts - I=1, V=5, etc. and -1 if the character is not a valid Roman Numeral
switch(letter)
{
case I: {return 1; break;
}
case V: {return 5; break;
}
case X: {return 10; break;
}
case L: {return 50; break;
}
case C: {return 100; break;
}
case D: {return 500; break;
}
case M: {return 1000; break;
}
default: {return -1; break;
}
}
public String toString() {
// Return the standard representation of this Roman numeral.
String roman = ""; // The roman numeral.
int N = num; // N represents the part of num that still has
// to be converted to Roman numeral representation.
for (int i = 0; i < numbers.length; i++) {
while (N >= numbers[i]) {
roman += letters[i];
N -= numbers[i];
}
}
return roman;
}
public int toInt() {
// Return the value of this Roman numeral as an int.
return num;
}

But I get an error with the int letterToNumber(char letter) (it says I need a semicolon there, which I really don't understand) and I'm not even sure if all of the code would work even if the brackets and semicolons were all in the right place. Am I on the right track?

And to stultuske - I'm a bit confused. I thought that was what toUpperCase did - I really am a beginner here. I thought the scanner did take the user's input and call it roman, then convert it to upper case? Is that not what I wrote?

well the problem is your 'int letterToNumber(char letter)' method is declared wrong it doesnt have an opening brace '{' and the other error most likely corresponds to it not having a closing '}' and/or at line 122 no '}' brace,it should be:

static int roman2Decimal(String number)
{
int[] numbers = { 1000, 900, 500, 400, 100, 90,
50, 40, 10, 9, 5, 4, 1 }; //it's much easier to program in the values for IV, IX, etc
String[] letters = { "M", "CM", "D", "CD", "C", "XC",
"L", "XL", "X", "IX", "V", "IV", "I" }; //than to use a bunch of 'if's and things to check if a // letter with a lower value is in front of one with a higher one.
final int num;
System.out.println("Please enter a roman numeral using the letters M, D, C, L, X, V, I. All letters will be treated as capitalized.");
System.out.println("Please do not enter a roman numeral with a value greater than 3999."); //3999 is the highest value since we're only
Scanner input = new Scanner(System.in);
String roman = input.next(); //using the roman numerals up to M, and you can't have 4 in a row.
roman = roman.toUpperCase();
roman = roman.trim();
if (roman.length()=0)
{
System.out.println("Please enter an actual number using the above mentioned roman numerals, not just empty spaces.");
roman = input.nextString();
}
int i = 0; //this will be our position in the string of the roman numerals.
int decimal = 0; //this is the decimal value of the roman numerals converted up until this point.
char letter;
int number;
while (i<roman.length())
{
letter = roman.CharAt(i); //the letter at the position i in the string roman
number = letterToNumber(letter); //The number that the letters represent - will be found later in the program.
if (number<0)
{
System.out.println("Illegal character " + letter + " in the string you entered. Please try again using ONLY M, D, C, L, X, V, and I.");
roman = input.nextString();
}
if (i == roman.length()) //There are no characters after where we are (at i), so just add the value of that letter to the number.
{
decimal += number;
}
else
{
int nextNumber = letterToNumber(roman.CharAt(i)); //move to look at the next letter in the string
if (nextNumber > number) //if the second number is greater than the one preceeding it, we need to subtract it from the original number.
{
decimal += (nextNumber - number);
i++;
}
else
{
decimal += number;
}
}
} //end while loop
if (decimal > 3999)
{
throw new NumberFormatException("The roman numeral should have a value strictly less than 4000.");
}
num = decimal;
int letterToNumber(char letter){
//This will convert the Roman Numerals to their decimal counterparts - I=1, V=5, etc. and -1 if the character is not a valid Roman Numeral
switch(letter)
{
case I: {return 1; break;
}
case V: {return 5; break;
}
case X: {return 10; break;
}
case L: {return 50; break;
}
case C: {return 100; break;
}
case D: {return 500; break;
}
case M: {return 1000; break;
}
default: {return -1; break;
}
}
}
public String toString() {
// Return the standard representation of this Roman numeral.
String roman = ""; // The roman numeral.
int N = num; // N represents the part of num that still has
// to be converted to Roman numeral representation.
for (int i = 0; i < numbers.length; i++) {
while (N >= numbers[i]) {
roman += letters[i];
N -= numbers[i];
}
}
return roman;
}
public int toInt() {
// Return the value of this Roman numeral as an int.
return num;
}
}

Here it is with the corrections that I've done until now.

look here:

if(roman.length()=0){}

cant compare using '=' must use '=='.

also look at your method roman2Decimal()

int number;

you cant do this because a variable name number already exists as an argument to 'int roman2Decimal(String number)' method. so you got to rename it.

also

letter = roman.CharAt(i);

is wrong it must be

letter = roman.charAt(i);//lowercase 'c'

this is also present in another place in your code, also look at your toString method where is int numbers and char/string letters declared or passed to the method? like this maybe

public toString(int num,int[] numbers,char[] letters){}

Thats just a few of the errors....

your roman2Decimal() method also is missing a closing brace '}', which should be inserted after 'num = decimal;' hence your method letterToNumber is messing around also declare your letterToNumber() method as static.after changing that you should return an int value from roman2Decimal() method-most likely num so use

'return num;'

, also your toInt() method is returning num but num was never declared in that method or passed to it-tofix this use the same rules as for the toString() method as above.

also in your switch statement you have

return 1000;
break;

how must it reach the code 'break' ever if its already returned? so you dont need break;

lastly on the note of your switch statement:

case I: {}

this is wrong, what on earth is I? it must be a char remember thats what you are comparing and switching

Hi. so this is actually a continuation from another question of mine[Here](https://www.daniweb.com/programming/software-development/threads/506795/dynamically-add-values-into-datagridview-cell-from-listbox-vb2010) but i was advised to start a new thread as the original question …

I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...

Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...