A
Alex
Hi,
I am just starting out and have what is probably a silly question but
any help would be appreciated.
I am trying to run the first example in the Java In a Nutshell 5th
edition book. Here is the code:
/**
* This program computes the factorial of a number
*/
public class Factorial { // Define a class
public static void main(String[] args) { // The program starts here
int input = Integer.parseInt(args[0]); // Get the user's input
double result = factorial(input); // Compute the factorial
System.out.println(result); // Print out the result
}
public static double factorial(int x) { // This method computes
x!
if (x < 0) // Check for bad
input
return 0.0; // If bad, return
0
double fact = 1.0; // Begin with an
initial value
while(x > 1) { // Loop until x
equals 1
fact = fact * x; // Multiply by x
each time
x = x - 1; // And then
decrement by x
} // Jump back to
start of loop
return fact; // Return the
result
} // factorial()
ends here
} // The class
ends here
Using the JDK I type "javac Factorial.java" and it seems to compile ok
and creates Factorial.class. However when I try to run that with "java
Factorial 4" I get the error message:
Exception in thread "main" java.lang.NoClassDefFoundError: Factorial.
I am not sure what I have done wrong. The code is exactly as it is in
the book and I even downloaded it from their website and tried it and
the same thing happened. What am I doing wrong?
Thanks,
Alex
I am just starting out and have what is probably a silly question but
any help would be appreciated.
I am trying to run the first example in the Java In a Nutshell 5th
edition book. Here is the code:
/**
* This program computes the factorial of a number
*/
public class Factorial { // Define a class
public static void main(String[] args) { // The program starts here
int input = Integer.parseInt(args[0]); // Get the user's input
double result = factorial(input); // Compute the factorial
System.out.println(result); // Print out the result
}
public static double factorial(int x) { // This method computes
x!
if (x < 0) // Check for bad
input
return 0.0; // If bad, return
0
double fact = 1.0; // Begin with an
initial value
while(x > 1) { // Loop until x
equals 1
fact = fact * x; // Multiply by x
each time
x = x - 1; // And then
decrement by x
} // Jump back to
start of loop
return fact; // Return the
result
} // factorial()
ends here
} // The class
ends here
Using the JDK I type "javac Factorial.java" and it seems to compile ok
and creates Factorial.class. However when I try to run that with "java
Factorial 4" I get the error message:
Exception in thread "main" java.lang.NoClassDefFoundError: Factorial.
I am not sure what I have done wrong. The code is exactly as it is in
the book and I even downloaded it from their website and tried it and
the same thing happened. What am I doing wrong?
Thanks,
Alex