Java programming help

Rockjay420

Gawd
Joined
Sep 29, 2005
Messages
732
Aloha,
Im in a intro to programming class, and Im stuck on this one.
I supposed to convert a year into roman numerals, but I have some bugs
that i cant seem to shake
Im getting "identifier expected" and variable not found messages from netbeans.

Here is my code, I suspect its something simple, like a misplaced { }

////////////////////////////////////////////////////////////////////////
// Program Name: romanNumerals
// Date: 04/24/2012
// Purpose: Assignment 4
// Author: Sanjay Ramsarran
////////////////////////////////////////////////////////////////////////
import java.util.Scanner;

public class YearToRomanNumerals
{
public static void main(String[] args)
{
YearToRomanNumerals convert= new YearToRomanNumerals();

Scanner keyboard= new Scanner(System.in);

System.out.println("Please enter a year between 1000 and 9999");

String year = keyboard.nextLine();

year= args[0];
convert.convertYear(year);
{
if (year.length()<4 || year.length()>4)
System.out.println("Year must be between 1000 and 9999.");

else
convert.convertYear(year);
}

private void convertYear(String year);
{
String romanNumeral= findRomanNumeral(year);
System.out.println("Year "+year+ " equals "+romanNumeral+" in Roman Numerals");
}
private String findRomanNumerals(String year)
{
String ones,
tens,
hundreds,
thousands,
romanNumeral;

ones= findOnes(year);
tens= findTens(year);
hundreds= findHundreds(year);
thousands= findThousands(year);

romanNumeral= thousands+hundreds+tens+ones;

return romanNumeral;
}

String findOnes(String year)
{
String romanNumeral= "";


switch(year.charAt(3))
{ case '0':
break;
case '1':
romanNumeral= "I";
break;
case '2':
romanNumeral= "II";
break;
case '3':
romanNumeral= "III";
break;
case '4':
romanNumeral= "IV";
break;
case '5':
romanNumeral= "V";
break;
case '6':
romanNumeral= "VI";
break;
case '7':
romanNumeral= "VII";
break;
case '8':
romanNumeral= "VIII";
break;
case '9':
romanNumeral= "IX";
break;

}

return romanNumeral;
}

String findTens(String year)
{ String romanNumeral= "";

switch(year.charAt(2))
{ case '0':
break;
case '1':
romanNumeral= "X";
break;
case '2':
romanNumeral= "XX";
break;
case '3':
romanNumeral= "XXX";
break;
case '4':
romanNumeral= "XL";
break;
case '5':
romanNumeral= "L";
break;
case '6':
romanNumeral= "LX";
break;
case '7':
romanNumeral= "LXX";
break;
case '8':
romanNumeral= "LXXX";
break;
case '9':
romanNumeral= "XC";
break;

}

return romanNumeral;
}

String findHundreds(String year)
{ String romanNumeral= "";

switch(year.charAt(1))
{ case '1':
romanNumeral= "C";
break;
case '2':
romanNumeral= "CC";
break;
case '3':
romanNumeral= "CCC";
break;
case '4':
romanNumeral= "CD";
break;
case '5':
romanNumeral= "D";
break;
case '6':
romanNumeral= "DC";
break;
case '7':
romanNumeral= "DCC";
break;
case '8':
romanNumeral= "DCCC";
break;
case '9':
romanNumeral= "CM";
break;

}

return romanNumeral;
}

String findThousands(String year)
{
String romanNumeral= "";

switch(year.charAt(0))
{ case '1':
romanNumeral= "M";
break;
case '2':
romanNumeral= "MM";
break;
case '3':
romanNumeral= "MMM";
break;
case '4':
romanNumeral= "MMMM";
break;

}
return romanNumeral;
}
}
 

MrWizard6600

Supreme [H]ardness
Joined
Jan 15, 2006
Messages
5,779
What IDE are you using? Because theres really only one right answer to that question: eclipse. --and I'm a VS.net guy.

and if you take a look at your IDE's output window it should also give you a line number.

something along the lines of:
YourProject/src/YearToRomanNumerals.java.43 identifier expected
43 being the line number.

Anyways, copy pasta'd into eclipse and it spits out the following:

  1. "main" is dangling, theres no closing brace "}", put one on line 29.
  2. I dont actually know if Java supports claus-less braces, (lines 22 and 28), but they serve no purpose, so you can remove them, they're not part of the if-else syntax. edit: it does, so you can leave them.
  3. Its considered by many to be bad practice to leave if-else statements without braces, so lines 23-27 should look like:
    Code:
    			if (year.length() < 4 || year.length() > 4){
    				System.out.println("Year must be between 1000 and 9999.");
    			}
    			else{
    				convert.convertYear(year);
    			}
  4. Your method "ConvertYear()" on line 31 has a semi-colon after it, it shouldn't. A method definition should never have a semi-colon after it.
  5. At line 33 you're referenceing a method findRomanNumeral that does not exist (you probably mean findRomanNumerals

And this is supposed to be a learning experiance for you so I'm going to intentionally not give too much away, but theres a couple things that might help you out
the "signature" of a java main method
public static void main(String[] args)
is a relic of old C-like languages. "args" is stuff you get if you were to run your program from commandline, which you probably aren't doing.

When you start debugging your instinct is going to be to System.Out.Println() everything, dont. In development environments thats simply not practical. "Debuggers" in IDE's these days are amazing, you should use them. This looks like a good concise tutorial on how to get started with the eclipse debugger --note that most other IDE's debuggers, including Visual Studios, are very similar.
 
Last edited:
Top