1

I'm trying to implement Banker's Algorithm in Java, but I'm having trouble loading my arrays. Here is the code I'm working with

public static void main(String[] args) throws FileNotFoundException {
    String filename = null;
    int need[][];
    int allocate[][];
    int max[][];
    int available[][];
    int n = 0;
    int m = 0;
    int lineCount = 0;
    Scanner in = new Scanner(System.in);

    System.out.println("Enter the file name.");
    filename = in.nextLine();

    File textFile = new File(filename);
    Scanner input = new Scanner(textFile);

    max = new int[n][m];
    allocate = new int[n][m];
    need = new int[n][m];
    available = new int[1][m];

    n = input.nextInt();
    m = input.nextInt();
    System.out.print("Number of Processes: " + n);
    System.out.print("\nNumber of Processes: " + m);

    max = new int[n][m];
    allocate = new int[n][m];
    need = new int[n][m];
    available = new int[1][m];

    String line = input.nextLine();

    while (line != null && lineCount < n) {

        String[] temp = line.split(" ");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                allocate[i][j] = Integer.parseInt(line);
                System.out.println("here");
            }
            line = input.nextLine();
            lineCount++;
        }
    }
}

My sample file contains this data.

5

4

0 0 1 2 1 0 0 0 1 3 5 4 0 6 3 2 0 0 1 4

0 0 1 2 1 7 5 0 2 3 5 6 0 6 5 2 0 6 5 6

1 5 2 0

1:0 4 2 0

So, I've had a number of different errors while trying to do this. Right now I'm getting an NumberFormatException: For input string "" error. Any help is much appreciated.

jynx678
  • 229
  • 3
  • 13
  • 1
    Could you explain the format of the input file? It seems not consistent (e.g., a colon in the last row only). Also, could you explain what `n` and `m` is, as well as the intuition of your codes? – Tsung-Ting Kuo Dec 01 '15 at 00:50
  • 1
    it would be helpful if you could post the stack trace of the ArrayIndexOutOfBoundsException and say which line number the exception is pointing to – Andy Guibert Dec 01 '15 at 00:52

1 Answers1

0

You have a bunch of really small arrays, and you never increase the sizes of them:

int n = 0;
int m = 0;
...
max = new int[n][m];       // 0x0 array, unused
allocate = new int[n][m];  // 0x0 array, likely the culprit
need = new int[n][m];      // 0x0 array, unused
available = new int[1][m]; // 1x0 array, unused

Of these arrays, only allocate is used, and you are using it later in a for loop:

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            // OutOfBounds is probably here:
            allocate[i][j] = Integer.parseInt(line);
            System.out.println("here");
        }
        line = input.nextLine();
        lineCount++;
    }

Also, you are running Integer.parseInt(line) which is attempting to parse the whole line. You should just parse a single token at a time which would be Integer.parseInt(temp[someindex]).

Andy Guibert
  • 34,857
  • 7
  • 32
  • 54
  • you're right of course. So I moved the assignment statements after n and m get their values and now I'm getting a NumberFormatException. The file has blank lines in it. – jynx678 Dec 01 '15 at 00:57
  • ok, so now you just need to do a bit of debugging -- start by printing out each line in your for loop, so you know which line the issue is on. Instead of printing out "here", try printing out `line` instead. The numberFormatException is probably on the last line where you have `1:0` – Andy Guibert Dec 01 '15 at 00:59
  • I changed the txt file (deleted 1:0) and I get the same error. I tried printing out LINE in the loop, same error. I moved it outside the loop, same error. It says For input string: "" – jynx678 Dec 01 '15 at 01:08
  • Does it have something to do with the spaces between the numbers in the file? – jynx678 Dec 01 '15 at 01:10
  • It's ok that there are spaces. You are doing `String[] temp = line.split(" ")` which splits a String up at each space. So if you have the String `"1 2 3 4"` and do split(" ") on it, you will get an array {"1", "2", "3", "4"} – Andy Guibert Dec 01 '15 at 01:12
  • Is it the `parseInt` that is throwing the exception? – jynx678 Dec 01 '15 at 01:17
  • @StephaniePrecht you should really make separate questions instead of updating your original post with the new issues -- now the original answer I gave no longer answers the question since it has been edited with a different problem – Andy Guibert Dec 01 '15 at 14:07
  • Sorry! won't do it again. Thanks for your help. – jynx678 Dec 02 '15 at 00:49