Reputation: 37
I have the following program that is supposed to be running but it is not. The professor gave it to us to work with. It is designed to calculate factorials. It won't even let me compile. The error I am getting states
Multiple markers at this line - Syntax error on token "Invalid Character", invalid Expression - Syntax error on tokens, delete these tokens
This is in reference to the following line:
System.out.print ( “%d! = %d\n”, counter, factorial (counter ));
How can I fix this? I've written quite a few programs before but I've never seen that modulus operator inside quotations before. I'm a little confused! The entire program is posted below! Thank you!
public class FactorialTest
{
// calculate the factorial of 0 – 15
public static void main ( String args[] )
{
FactorialCalculation factorialCalculation = new FactorialCalculation();
factorialCalculation.displayFactorials();
} // end of main
} // end of the class FactorialTest
public class FactorialCalculation
{
//recursive Factorial method
public long factorial(long number)
{
if (number <= 1)
return 1;
else
return number * factorial (number - 1);
}
//Now output the factorials of 0 through 15
public void displayFactorials()
{
// Calculate the factorial of o through 15
for ( int counter = 0; counter <= 10; counter++ )
System.out.print ( “%d! = %d\n”, counter, factorial (counter ));
} // end of the method displayFactorials
} // end of class FactorialCalculation
Upvotes: 0
Views: 201
Reputation: 6450
You need to replace the fancy curly braces
and also you need to format the string using printf
to get the output which you are expecting.
public class FactorialTest {
// calculate the factorial of 0 – 15
public static void main(String args[]) {
FactorialCalculation factorialCalculation = new FactorialCalculation();
factorialCalculation.displayFactorials();
} // end of main
} // end of the class FactorialTest
class FactorialCalculation {
//recursive Factorial method
public long factorial(long number) {
if (number <= 1)
return 1;
else
return number * factorial(number - 1);
}
//Now output the factorials of 0 through 15
public void displayFactorials() {
// Calculate the factorial of o through 15
for (int counter = 0; counter <= 10; counter++) {
System.out.printf("%s! = %s\n", counter, factorial(counter));
}
} // end of the method displayFactorials
}
Note: you don't need to define public
for FactorialCalculation
class. This restriction is not yet enforced by the compiler, although it's necessary for efficient package importation.
We can have only one top level public either class or interface in any java compilation unit ( .java source file )
Upvotes: 0
Reputation: 40315
You have this:
“%d! = %d\n”
The “ and ” fancy quote characters are not valid quote characters in Java. Use a plain old " instead.
If your editor is automatically creating fancy quotes for you, either disable that or find a more appropriate editor.
Upvotes: 1