-1

I have looked at other questions about out of bounds error and understood them, couldn't understand the fault in this code.

Question: A JAVA program that will read a boolean matrix corresponding to a relation R and output whether R is Reflexive, Symmetric, Anti-Symmetric and/or Transitive. Input to the program will be the size n of an n x n boolean matrix followed by the matrix elements.

The program must output a reason in the case that an input relation fails to have a certain property.

Solution: I have provided the code, it throws the "java.lang.ArrayIndexOutOfBoundsException" error at main and line 65. I can't see how my arrays are out of bounds

ERROR: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at XYZ.BooleanMatrix.main(BooleanMatrix.java:65)

Code:

package XYZ;
import edu.princeton.cs.algs4.*;
public class BooleanMatrix {

// read matrix from standard input
public static boolean[][] read() {
    int n = StdIn.readInt();
    boolean[][] a = new boolean[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (StdIn.readInt() != 0)
                a[i][j] = true;
        }
    }
    return a;
}

// print matrix to standard output
public static void print(boolean[][] a) {
    int n = a.length;
    StdOut.println(n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (a[i][j])
                StdOut.print("1 ");
            else
                StdOut.print("0 ");
        }
        StdOut.println();
    }
}

// random n-by-n matrix, where each entry is true with probability p
public static boolean[][] random(int n, double p) {
    boolean[][] a = new boolean[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            a[i][j] = StdRandom.bernoulli(p);
        }
    }
    return a;
}

// display the matrix using standard draw
// depending on variable which, plot true or false entries in foreground
// color
public static void show(boolean[][] a, boolean which) {
    int n = a.length;
    StdDraw.setXscale(0, n - 1);
    StdDraw.setYscale(0, n - 1);
    double r = 0.5;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (a[i][j] == which) {
                StdDraw.filledSquare(j, n - i - 1, r);
            }
        }
    }
}

// test client
public static void main(String[] args) {
    int n = Integer.parseInt(args[0]); //LINE 65
    double p = Double.parseDouble(args[1]);
    boolean[][] a = random(n, p);
    print(a);
    show(a, true);
}}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    and what is line 65? – Stultuske Oct 17 '17 at 10:49
  • did you specify the arguments? – Maurice Perry Oct 17 '17 at 10:56
  • Please provide the full stackexception in your question. – AxelH Oct 17 '17 at 10:57
  • 2
    ... on the command line? – Maurice Perry Oct 17 '17 at 11:03
  • 2
    @MauricePerry has a point. If you didn't give any command line arguments, `args` (in `main`) would have length zero and referencing `args[0]` would throw `IndexOutOfBoundsException`. – Kevin Anderson Oct 17 '17 at 11:04
  • I have edited the question to indicate line 65 and the errors. I am working on what you guys said. – user4127994 Oct 17 '17 at 11:11
  • From your edit, you are clearing not passing any argument. So `args` is empty. use the command `java BooleanMatrix arg1 arg2` to pass your value. See [Command-Line Arguments](https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html) – AxelH Oct 17 '17 at 11:13
  • Thank you @AxelH I tried the command in the terminal window, it said no such file or directory found. Am I to go in a certain directory and then execute the command? Also, will adding a try-catch instead work? – user4127994 Oct 17 '17 at 11:34
  • go to the root of the project, the directory where the folder _XYZ_ is then call java XYZ.BooleanMatrix arg1 arg2. (you need to compile it first), see [this thread](https://stackoverflow.com/questions/19433366/running-java-in-package-from-command-line) – AxelH Oct 17 '17 at 12:18

1 Answers1

0

I don´t know the exact working of StdDraw.setXscale(0, n - 1); but i think it creates a table with n-1 rows. so if you try to fill it with n rows there will be an out of bounds error. try using this in line 47:

StdDraw.setXscale(0, n);
StdDraw.setYscale(0, n);

As stated in the comments below your post: if you don´t enter any arguments when calling the program you´ll get an out of bounds exception because the program expects arguments in the aray and there aren´t any.

To provide arguments open command line and call /java yourcompiledjavafile arg[0] arg[1]

NotMyFaultSir
  • 173
  • 11
  • It could be a good idea, but i don't think it's the cause for the exception – Maurice Perry Oct 17 '17 at 11:08
  • I tried changing around a lot of the values but did not work. How am I to provide the arguments in the main? – user4127994 Oct 17 '17 at 11:13
  • I tried the command in the terminal window, it said no such file or directory found. Am I to go in a certain directory and then execute the command? Can I add a try-catch instead? – user4127994 Oct 17 '17 at 11:23
  • you can add a try catch but you`ll always end in the catch because you´ll never have arguments. you have to compile your file first: javac yourfile.java java yourfile – NotMyFaultSir Oct 17 '17 at 11:52
  • Thank you @NotMyFaultSir I tried but got errors, I am not familiar with using the command line for compiling java, I use a MAC and read some documents just now and tried multiple commands in Terminal, did not help. Is there any way to pass the arguments in Eclipse itself? – user4127994 Oct 17 '17 at 12:05