views:

87

answers:

3

I do all my programming assignments in eclipse first before putting them in putty and submitting them to our teacher. In eclipse, I have this strange error in one of my methods.

it says "Syntax error on token(s), misplaced construct(s)."

public static int factorial(int iVal, boolean DEBUG)
{
 int result;
    // Defensive programming
 if(iVal <= 0)
 {
  System.out.println("Error: iVal cannot be a negative number!");
  System.exit(0);
 }
    // Calculate result
 int factor = iVal;
 int counter = iVal - 1;
 for(int i = counter; i > 1; i--)
 {
  if(DEBUG = true)
  {
   System.out.println("DEBUG");
    System.out.println("   Finding the factorial of " + factor);
   System.out.println("   Currently working on " + i);
   System.out.println("   With an intermediate result of" + iVal);
  }
  iVal *= i;
 }
       result = iVal;
    // Return result
       return result;
} // End of factorial method

It has the error placed on the line consisting of

System.out.println("   Currently working on " + i);

Any ideas?

+8  A: 

I'm guessing you don't want to perform an assignment in an if-statement:

if(DEBUG = true) {

Did you mean:

if(DEBUG == true) {

?

Emil H
that sure is a valid point, but it is not causing a compilation error.
Bozho
Thanks for that, you saved me some headaches when compiling lol. But it still did not fix the construct error.
John
@John: Then you'll have to provide more of you code, as the actual compilation error doesn't appear to be in the code sample you have provided. And see unholysampler's answer for a better way to code this.
GreenMatt
I agree with Bozho and GreenMat. I don't see any other syntax errors.
Emil H
+3  A: 
if(DEBUG = true)

Comparison is ==, assignment is =.
Also, if you are just testing a boolean value, you don't need to do a comparison at all and just use

if(DEBUG)
unholysampler
+1 for just using if (DEBUG)
GreenMatt
+1  A: 

You have an assignment in an if statement:

if(DEBUG = true){

This is legal (and compiles) because DEBUG is of type boolean, but it is always true.

Peter Knego