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?

## All 15 Replies

The code tags were not properly placed its hard to traverse the code... the tag /code] is missing an [
Also I don't you should initialize values in a loop

roman.toUpperCase???
I assume you'll use your instance of Scanner (roman) as a means to read a String instance, which you'll then set to UpperCase

and why this:
return -1; break;
return automatically ends the method by returning the value.

I'm sorry the code didn't go into the brackets properly - I've never posted code before. My mistake. I tried to fix it, but apparently I can't edit a post until 30 minutes after I've posted it...

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?

Hold on! I take that back - just a second. How's this?

``````Scanner input = new Scanner(System.in);
roman = input.nextInt();
roman = roman.toUpperCase();
roman = roman.trim();``````

Hold on! I take that back - just a second. How's this?

``````Scanner input = new Scanner(System.in);
roman = input.nextInt();
roman = roman.toUpperCase();
roman = roman.trim();``````

I was gonna reply how to properly do it but it seems you got the gist of it :)
You can try it to see if it works for you

Thanks! I still get the two error messages I got before, namely:
StringMethods.java:75: ';' expected
int letterToNumber(char letter)
^
StringMethods.java:122: '}' expected
}

Do you have an idea what the semicolon error might be about?

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:

``````int letterToNumber(char letter){
//work for method here

}``````

I still get the same error even though I corrected that. Thank you for pointing that out, but something else must be wrong, too, I suppose.

I still get the same error even though I corrected that. Thank you for pointing that out, but something else must be wrong, too, I suppose.

Maybe repost your code properly this time and then we can help further

Hold on! I take that back - just a second. How's this?

``````Scanner input = new Scanner(System.in);
roman = input.nextInt();
roman = roman.toUpperCase();
roman = roman.trim();``````

this won't work the way you want.
try this:

``````Scanner input = new Scanner(System.in);
String roman = input.next();
roman = roman.toUpperCase();
roman = roman.trim();``````

nextInt() will read the first primitive int, which does not really do what you want to have :)

``````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.

Thank you!

Thank you!

does this mean the problem is solved, or are there still questions?

``````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

``case 'I': {}``

do this for all of them they are all chars

lastly i see you have this in a few places

``roman = input.nextString();``

there is no such method it should be:

``roman = input.nextLine();``

after all that you should be solved:)

Oh my goodness- thank you so much! I will call this thread solved. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.