Java help with xcode

Discussion in 'Mac Programming' started by phiber optik, Apr 25, 2006.

  1. macrumors newbie

    Joined:
    Apr 25, 2006
    #1
    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;
    }
    }
     
  2. macrumors regular

    Joined:
    Apr 11, 2003
    #2
    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
     
  3. macrumors 6502

    Joined:
    May 12, 2003
    #3
    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.
     
  4. thread starter macrumors newbie

    Joined:
    Apr 25, 2006
    #4
    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!
     
  5. macrumors 6502

    Joined:
    May 12, 2003
    #5
    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.
     
  6. macrumors regular

    Joined:
    Apr 11, 2003
    #6
    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.
     
  7. macrumors 6502

    Joined:
    May 12, 2003
    #7
    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
     
  8. thread starter macrumors newbie

    Joined:
    Apr 25, 2006
    #8
    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!
     
  9. macrumors 6502

    Joined:
    May 12, 2003
    #9
    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.
     
  10. thread starter macrumors newbie

    Joined:
    Apr 25, 2006
    #10
    it works!! amazing! thanks for the help.
    oh, and for those who were curious, the factorial of 8 is 40320 :D
     

Share This Page