0
import java.util.Arrays;

public class Lab12st {
    public static void main(String args[]) {
        System.out.println("/nLab 12 80 Point Version \n");
        final int MAX = 100;
        boolean primes[];
        primes = new boolean[MAX];
        computePrimes(primes);
        displayPrimes(primes);
    }

    public static void computePrimes(boolean listA[]) {
        Arrays.fill(listA, true);

        for (int j = 2; j < 100; j++) {
            for (int k = 0; k <= 100; k += j) {
                listA[k] = false;
                System.out.println(listA[k + 1]);
            }
        }
    }
}

I have tried using different relational operators, switching some numbers around, and I still get an IndexOutofBounds error. I think it is because I have 100 array elements that are listed 0-99, but I don't know how to fix this. Any help would be greatly appreciated.

Tomasz Dzieniak
  • 2,765
  • 3
  • 24
  • 42

2 Answers2

2

This line is your problem.

    for (int j = 2; j < 100; j++)
    for (int k = 0; k <= 100; k += j)
    and 
    System.out.println(listA[k + 1]);    

100 would give you the number of bool in the array. However, the largest legal index is always the length()-1.

Thus, you are trying to access a character that is outside of the array, resulting in the indexOutOfBounds, as indexes always count 0 as a place.

The fact that you are hardcoding the 100 values into the code is bad practice.

You should use

listA.length

Thus, your solution would be

for (int j = 2; j < listA.length; j++)
    {
        for (int k = 0; k <listA.length; k += j)
        {
            listA[k] = false;
            if(k >= listA.length)
            {

                 System.out.println("THERE ARE NO VALUES AT K+1");
                  //Add what you want to do here if there are no more       values.                  
                   break; //this is a typical solution.
            }
        }
    }

if you use the comparator "<=" then you must use listA.length-1

DarkV1
  • 1,776
  • 12
  • 17
2

In your inner loop the condition is causing issues

 for (int k = 0; k <= 100; k += j)

At each iteration the index k is incremented of a value equal to j

Since the array size is 100 at some point you will get the index-out-of-bounds error.

My question is why are performing such increment? What is your code actually doing?

In addition to that you should keep an eye on this line of code and adjust the condition of your for loop accordingly

System.out.println(listA[k + 1]);
Angelo Oparah
  • 673
  • 1
  • 7
  • 18
  • `System.out.println(listA[k + 1])` will still throw the exception – JohnnyAW May 08 '16 at 21:54
  • @JohnnyAW I'm a aware of that: I just wanted to post a quick reply and then going to edit my answer – Angelo Oparah May 08 '16 at 22:01
  • @AngeloOparah I wouldnt suggest the "100" in the code as hard coding is bad practice . list.length is solution – DarkV1 May 08 '16 at 22:03
  • We are doing a way of computing primes using a method called Sieve of Eratosthenes method. You start with 2, then you change boolean values that are equal to multiples of 2 to false, same with 3, and so on until you reach 100. All of the values that are left as true within the program are all of the times between 1 and 100 – D. Rodriguez May 09 '16 at 15:17
  • @D.Rodriguez ok now that makes sense. Thanks for sharing – Angelo Oparah May 09 '16 at 15:39