Bryce
Bryce

Reputation: 13

Having trouble calling an array from one method to another in Java

The way I have my code set up is I declare my array in one method then I want to print it out in a table like fashion in another. But I want to do this while only using the main() function.

I have taken most of the irrelevant code out so here is my code:

public static void main(String[] array) {
    test2(array);
}

public static void test() {
    String[] array = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"};
}

public static void test2( String[] array ) {
    int count = 0;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) { 
            System.out.print(array[count] + "\t"); 
            count++;
        }   
        System.out.println();
        System.out.println();
        System.out.println();
    }
}

When I try and run it, it comes up with the java.lang.ArrayOutOfBound on the line "System.out.print(array[count] + "\t");"

Does anyone know why this is and/or how to fix it?

Upvotes: 1

Views: 68

Answers (2)

Alex Emexezidis
Alex Emexezidis

Reputation: 11

Well, it doesn't work because when your "main" calls "test2(array)" it passes nothing, since main doesn't have an "array" defined in it.

The array that you want exists only inside the "test" method.

So, one simple solution would be to change your code to:

public static void main(String[] array) {
    test2(test());
}

public static String[] test() {
    String[] array = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"};
    return array;
}

public static void test2( String[] array ) {
    int count = 0;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) { 
            System.out.print(array[count] + "\t"); 
            count++;
        }   
        System.out.println();
        System.out.println();
        System.out.println();
    }
}

...so that the method test() returns the array when it's called in main.

But, still, the code doesn't make lots of sense, especially in Object Oriented programming.

Upvotes: 0

Olivier Gr&#233;goire
Olivier Gr&#233;goire

Reputation: 35467

You have several errors:

  1. You create array as a local variable in test().
  2. You use the arguments of the application as parameters.
  3. You don't even call test().

The consequences are that you call your application probably with no parameters and end up having the test2() method trying to access the first element of an empty array, causing your exception.

This is what you should do, but keep reading after the code, I'm not done:

public static void main(String[] args) { // This array is defined, but don't use it.
  test2(test());
}

public static String[] test() {
  return new String[]{"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"};
}

public static void test2( String[] array ) {
  int count = 0;
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) { 
      System.out.print(array[count] + "\t"); 
      count++;
    }   
    System.out.println();
    System.out.println();
    System.out.println();
  }
}

This code has still issues. You indeed assume that you have 16 elements in your array. Yet you're not sure. Oh yes, you're sure because you've added them, but you shouldn't assume that it'll always be the case.

Therefore it's nice to check for the actual number of the elements anyways.

public static void test2( String[] array ) {
  int count = 0;
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      if (count < array.length) {
        System.out.print(array[count] + "\t"); 
        count++;
      }
    }   
    System.out.println();
    System.out.println();
    System.out.println();
  }
}

Upvotes: 3

Related Questions