-1

I've been trying to dive into java development and have gone for a relatively easy problem of finding prime numbers, however, I keep getting errors and can't see what I've done wrong, any help?

I've been toiling over my computer for an infuriating while and have tried everything, even rewriting the code from beginning

public class HelloWorld{

    public static void main(String []args){

        int[] check = {2};

        //cycle through numbers 1-100

        for (int i = 1; i < 100; i++) {
            //cycle through numbers to be checked against i
            for (int x = 0; x < 101; x++) {
                //check if the current itteration of i has no multiples
                if (i%check[x] == 0) {
                    check[i] = i;
                } else {
                    // print any prime numbers
                    System.out.print(i);
                    check[i] = i;
                }
            }
        }
    }
}
  • What's the purpose of check[] ? – Saurav Kumar Sep 24 '19 at 15:40
  • `check` has a length of 1. You're trying to access indices that don't exist – GBlodgett Sep 24 '19 at 15:41
  • try explaining your method first.. your `check int[]` has length=1, so `check[x]` will fail as soon as `x > 0` – diginoise Sep 24 '19 at 15:41
  • `check` only contains a single element. You can't index it with anything other than `0`, since that's the only index that exists in the array – Carcigenicate Sep 24 '19 at 15:41
  • 1
    Welcome to StackOverflow. Try to improve your question, by adding the exact errors you are getting (copy and paste them, not an image, text). While we sympathize with your hard work, telling us about it gives us no information useful for solving the problem, while giving us the expected results, what happens instead, and the error text, does. – RealSkeptic Sep 24 '19 at 15:41
  • I recommend doing basic tutorials and learning the basics of Java. If you had gone through a tutorial on matrices and learned how to use them, you could see why `int [] check = {2}` is silly for this case and unnecessary. – Nexevis Sep 24 '19 at 15:44

2 Answers2

0

The immediate cause of your error is that you defined the check[] array to have a size of 1, but you are trying to access elements higher than that, which don't exist. However, I don't think that you really need that array here. Consider this version:

for (int i=2; i < 100; i++) {
    boolean match = true;
    for (int x=2; x <= Math.sqrt(i); x++) {
        if (i % x == 0) {
            match = false;
            break;
        }
    }
    if (match) {
        System.out.println("Prime number: " + i);
    }
}

Note that the inner loop in x only needs to go as high as the square root of the outer i value. This is because any value greater than sqrt(i) can't possible divide it.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

It is because your array has length 1 and you are trying to access out of bound indexes. In case you are lopping 0 to 101 You can initialize your array like this int [] check =new int [101]

Habib Mhamadi
  • 729
  • 1
  • 6
  • 15
  • Yes but then what do you expect `i % check[x] == 0` to do. `check[x]` is going to access an `int` that was never initialized to any value. His logic is just completely wrong in general. – Nexevis Sep 24 '19 at 15:49