Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

phiber optik

macrumors newbie
Original poster
Apr 25, 2006
17
0
Hey guys i was having some trouble compiling/running some simple java code on my mac and i was wondering if you guys could help me out. This is very simple code to get me started with java, but whenver i run it i get this output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at factorial.main(factorial.java:13)

java has exited with status 1.

i really dont know what to do with the code or anything. Any help will be greatly appreciated. Here's the code.
import java.util.*;

public class factorial {

public static void main(String[] args) {
int input = Integer.parseInt(args[0]);
double result = factorial(input);
System.out.println(result);
}

public static double factorial(int x) {
if (x < 0)
return 0.0;
double fact = 1.0;
while(x > 1) {
fact = fact * x;

x = x - 1;
}
return fact;
}
}
 

Thom_Edwards

macrumors regular
Apr 11, 2003
240
0
are you running it with something like "java factorial 8"? if you just run it with "java factorial", there is no argument for args[], thus making any index out of bounds.

EDIT: i'm assuming it compiled okay, because your code compiled fine for me
 

therevolution

macrumors 6502
May 12, 2003
468
0
It must be this line:

int input = Integer.parseInt(args[0]);

That array, args, contains the arguments you put on the command line. It looks like you didn't put any. For example, you're probably typing this when you run it:

java factorial

when you need to put a number at the end:

java factorial 10

This will make the value at args[0] be 10.
 

phiber optik

macrumors newbie
Original poster
Apr 25, 2006
17
0
well, the only thing im doing to run this program is to click compile and run, im not typing any numbers in. How would i open the file with a number as an argument? Could i go into the terminal and run the program with that argument and do it that way?
Thanks for the help!
 

therevolution

macrumors 6502
May 12, 2003
468
0
Yes, you can do this from the Terminal. I don't know Xcode very well, so I have no idea how to do it there, sorry.
 

Thom_Edwards

macrumors regular
Apr 11, 2003
240
0
that's exactly how you would do it. you can't just double-click the .java file after compiling. it is a command-line application. go into terminal and type "java factorial x" where x is an integer.

and not to get on a soap box, but that is where some good error checking code would help. check that there is at least one index of args[] and that it is an int before you continue with the rest of the code. if both conditions are not met, you can output a friendly error message. as others might agree, i seem to spend almost as much time error checking as i do actually writing a program! not really, but it is an important aspect.
 

therevolution

macrumors 6502
May 12, 2003
468
0
Thom_Edwards said:
and not to get on a soap box, but that is where some good error checking code would help.
I agree, but you have to pick your battles when you're helping someone that's new to programming. Let's learn about command-line arguments before we move on to that...

Besides, a new programmer won't appreciate good error checking until they get burned a few times anyway. This is just one such time. Gotta let them learn the hard way. :D
 

phiber optik

macrumors newbie
Original poster
Apr 25, 2006
17
0
great. it looks like it works through the terminal, but im getting another error:
Exception in thread "main" java.lang.NoClassDefFoundError: factorial
i still get the same error if i dont even put in an argument
do i have to be in the same directory as the file or can i be anywhere when im running the code from terminal (im assuming anywhere)
thanks for the help!
 

therevolution

macrumors 6502
May 12, 2003
468
0
You need to be in the same directory.

If you go there and don't see a factorial.class file, you need to execute:

javac factorial.java

Then you will be able to run it as we've discussed.
 

phiber optik

macrumors newbie
Original poster
Apr 25, 2006
17
0
it works!! amazing! thanks for the help.
oh, and for those who were curious, the factorial of 8 is 40320 :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.