简体   繁体   中英

Index out of bounds Exception but can't figure out what is wrong

Basically I have to take user input until they enter 0 and then find the max, number of negative numbers, and sum of positive numbers in the array. The problem is I have to use and array and cannot use ArrayList to call the other methods so I thought I would create an ArrayList and use the elements of that to create an array since there is an unspecified number of elements in the array. I keep getting this as errors and have tried everything I could think of. Please help.

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.get(ArrayList.java:322)
    at Assignment9.assignment9(Assignment9.java:37)
    at Assignment9.assignment9(Assignment9.java:49)
    at Assignment9.assignment9(Assignment9.java:49)
    at Assignment9.assignment9(Assignment9.java:49)
    at Assignment9.main(Assignment9.java:17)

//Class Description: Takes user input and makes it into an array until 0 is entered.
// It then computes the maximum #, amount of negative #s and the sum of the positive #s.
import java.util.*;
import java.text.*;;

public class Assignment9 {

//main method initializes variables then calls method assignment9
public static void main(String[] args)
{
    int count = 0;
    ArrayList<Double> help = new ArrayList<Double>();
    assignment9(help, count);
}//end main

//adds user input to array until 0 is entered; it then calls other methods
public static void assignment9(ArrayList<Double> help, int count)
{
    DecimalFormat fmt = new DecimalFormat("#.##");
    double input;
    double max = 0;
    int negative = 0;
    double sum = 0;
    Scanner scan = new Scanner(System.in);

    input = scan.nextInt();

    if (input == 0)
    {
        double[] numbers = new double[help.size()];
        for(int i = 0; i < numbers.length; i++)
        {
            numbers[i] = help.get(i);
        }
        findMax(numbers, count, max);
        countNegative(numbers, count, negative);
        computeSumPositive(numbers, count, sum);
        System.out.println("The maximum numer is " + fmt.format(max));
        System.out.println("The total number of negative numbers is " + negative);
        System.out.println("The sum of positive numbers is " + fmt.format(sum));
        System.exit(0);
    }else{
            help.add(input);
            count++;
            assignment9(help, count);
    }//end if
}//end assignment9

//compares elements of array to find the max until count = -1
public static double findMax(double[] numbers, int count, double max)
{

    if (count == -1)
    {
        return max;
    }else if(numbers[count] > max){
        max = numbers[count];
        count--;
        findMax(numbers, count, max);
    }//end if
    return max;

}//end findMax

public static int countNegative(double[] numbers, int count, int negative)
{

    if(count == -1)
    {
        return negative;
    }else if(numbers[count] < 0){
        negative++;
        count--;
        countNegative(numbers, count, negative);
    }
    return negative;
}//end countNegative

public static double computeSumPositive(double[] numbers, int count, double sum)
{
     if(count == -1)
     {
         return sum;
     }else{
         sum = sum + numbers[count];
         count++;
         computeSumPositive(numbers, count, sum);
         return sum;
     }
}//end computeSumPositive

}//end class

The problem is that you are passing in the number count into findMax and it's throwing an ArrayIndexOutOfBoundsException while attempting to access the upper limit on this line:

} else if (numbers[count] > max) {

Remember arrays in Java are zero based. Also, as Java uses pass by value you need to assign the result to the output of findMax :

max = findMax(numbers, count - 1, 0);

Logically, findMax will always return the last number rather than the highest number. You need to return the temporary max from within the recursive method:

double findMax(double numbers[], int count, int index) {
   // This needs to be size - 1 because the array is 0-indexed.
   // This is our base case
   if (index == count - 1)
      return numbers[index];

   // Call the function recursively on less of the array
   double result = findMax(numbers, count, index + 1);

   // Return the max of (the first element we are examining, the max of the
   // rest of the array)
   if (numbers[index] > result)
      return numbers[index];
   else
      return result;
}

This version handles the count index upper limit internally:

max = findMax(numbers, count, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM