r/javahelp • • Jun 13 '20

ArrayIndexOutOfBoundsException: 0 error help. My code is exactly the same as the textbook I'm learning from (Algorithms by Sedgewick and Wayne).

Simple program to update count by 1 if three ints sum to 0, ints fed into program via a text file.

I understand (or I think I do) what the error means, basically the array is empty!

Here is the code:

The command I'm using in CMD to run program is: java ThreeSum < 1Kints.txt

public class ThreeSum
{
    public static int count(int[] a)
    {
        int n = a.length;
        int count = 0; 
        for (int i = 0; i < n; i++)
            for (int j = i+1; j < n; j++)
                for (int k = j+1; k < n; k++)
                    if (a[i] + a[j] + a[k] == 0)
                        count++; 
        return count; 
    }
    public static void main(String[] args)
    {
        In in = new In(args[0]); 
        int[] a = in.readAllInts();
        StdOut.println(count(a)); 
    }
}

Like I said, this is a direct copy from the textbook, the only thing I can think of is that the command to run this program and parse the file 1kints.txt to the In in = new In(args[0]) line is incorrect?

It's annoying because I have definitely had this program run in the past.

Sorry if this is really obvious, I cannot see, trying all day. This isn't even part of an exercise or anything.

18 Upvotes

9 comments sorted by

5

u/Stack_Canary Jun 13 '20

Have you tried running it simply like this: "java ThreeSum 1Kints.txt " ?

5

u/BillMurray2020 Jun 13 '20

Fuck sake. Yeah, that worked ha!

Clearly I need to re-read something in this textbook!

Thank you.

7

u/Stack_Canary Jun 13 '20

If you wanna know the reasoning:

String[] args is an array containing the command line arguments. ArrayIndexOutOfBound 0 means that the program tried to access an element on index 0 (the first element), but it was out of bounds. That can only mean that the array is empty,and the program thinks it does not have any command line arguments. Only explanation then is that ' < ' does something fucky (in this case its because < is a special symbol in CMD which is used to redirect input)

3

u/Roachmeister Java Dev Jun 13 '20

Exactly. The OS parses this:

java ThreeSum < 1Kints.txt

into two commands, one with one argument and one with zero arguments, separated by a redirect operator.

1

u/iamsooldithurts Jun 13 '20

No, it’s one command, it just redirects the program’s System.in (standard input stream) to come from the text file instead of keyboard.

2

u/Roachmeister Java Dev Jun 13 '20

From the standpoint of the OS, yes, it is one command. But from the standpoint of java, the OS only passes one parameter to it, which is ThreeSum. The other parameters, < and 1Kints.txt, are stripped off and never passed in to the program, hence the OP's exception.

4

u/FatnDrunknStupid Jun 13 '20

Anyone else going whaaa.....t?

1

u/BillMurray2020 Jun 13 '20

No. Let me try that.

2

u/mtko Jun 13 '20

Yea I think your args are wrong because of the < in your command.

You can check your args with something like this in your main method:

for (int i = 0;  i< args.length; i++) {
    System.out.printf("args[%d] = %s%n", i, args[i]);
}