0

I have a program below where an array contains some int values. It returns the array in a sequence depending whether the int value is a multiple of 10. This program does that fine, but how would I check to see if the array has no int values inside it? I get an error (Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0) Thankyou!

public class Test1{
   public static void main(String[] args){
      int[] nums = {};
      int[] array = tenRun(nums);
      int i;
      for(i = 0; i < array.length - 1; i++) 
         System.out.print(array[i] + ", ");
      System.out.println(array[i]);
   }

   public static int[] tenRun(int[] nums) {
      int current;
      int i = 0;

      while(i < nums.length && nums[i] % 10 != 0)
         i++;   
      if(i >= nums.length)
         return nums;

      current = nums[i]; 
      i++;

      while(i < nums.length) {
         if(nums[i] % 10 == 0)
            current = nums[i];
         else
            nums[i] = current;
         i++;
      }                                                      
      return nums; 
   }
}

2 Answers 2

1

First of all, it's not good style to declare an array with {}.

If anybody could call tenRun(int[] nums), then I'd probably check to see if it is empty or null like this:

if(nums == null || nums.length == 0)

but since you only want your code to run when it's not null and when it's not empty, do it like this:

if(nums != null && nums.length > 0) {
    //code here
}

Your for loop in main should look like:

//get rid of int i;
if(array.length > 0) {
    System.out.println("{");
    for(int i = 0; i < array.length - 1; i++) {
        System.out.print(array[i] + ", ");
    }
    System.out.println("}" + array[i]);
} else {
    System.out.println("{}");
}  
Sign up to request clarification or add additional context in comments.

5 Comments

I still can't get my head around it, I think I might be over complicating it for myself. If I have an array of {8, 40, 7, 9, 30, 6, 4, 40, 1, 9} it returns 8, 40, 40, 40, 30, 30, 30, 40, 40, 40 as expected but if someone leaves the array blank {} it still throws the exception.
@SombraDevon I figured it out. You have a fencepost problem on your hands. You reach the end of the loop where i = array.length -1, but then you try to print out array[array.length] because the for loop still tries to increment i.
Thanks! That fixes it! However, would it be possible to check for that in the tenRun method?
Well, actually the problem was with you main method the whole time, but the tenRun method should probably check for nulls. The reason for this was because you were trying to print array[0] with an empty array, and that's what caused the error all along, so no, you have to check for it in main.
No problem and good luck. BTW you can mark your question as answered so that way people know that they don't need to look at this question anymore.
0

check the array length before printing or accessing elements in it, in your code at the end of main you are printing array element without check. include a check so that you can avoid this error , once tenRun returns an array check size of it

if(array.length > 0 )
{  int i;
   for(i = 0; i < array.length - 1; i++) 
       System.out.print(array[i] + ", ");
}
else
{
    System.out.println("Received empty array");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.