0

I'm sorry for (another) admittedly quite basic question, but I'm having more problems in some Java code that I have to right for class. The assignment is to do a sieve of Eratosthenes program, and while my constructor seems to work (calling myArray[50] from the main method gives the value it was initialized to), it doesn't reflect any changes that the other classes were meant to make to myArray. Also, print() prints nothing (probably because the function sees an unitialized version of myArray.).

The goal is that SieveEm() and deleteStuff() mark all of the non-primes as false, the print() comes by and prints all of the prime numbers(marked true).

I couldn't seem to find anything on the internet/StackOverflow addressing the problem. What am I doing wrong?

    public class Sieve {
    private int count;
    private boolean[] myArray;
    public Sieve(int length){
        count = length+1;
        boolean[] newArray = new boolean[length+1];
        if(length>=1){
            newArray[0]=false;
            newArray[1]=false;
            if(length>=2){
                for(int i=0;i<=length;i++){
                    newArray[i]=true;
                }
            }
        }
        this.myArray = newArray;
    }
    public void SieveEm(){
        System.out.println("here now");
        System.out.println(this.myArray[50]==true);
        for(int i=0; i<count;i++){
            System.out.println("We got this far");
            if(this.myArray[i]){
                deleteStuff(i);
            }
        }
    }
    public void deleteStuff(int current){
        int increment= current;
        current+=increment;
        while(current<count){
            this.myArray[current]=false;
            current+=increment;
        }
    }
    public void print(){
        this.SieveEm();
        for(int i=2;i<count;i++){
            if(this.myArray[i]){
                System.out.println(i);
            }
        }
    }
    public static void main(String[] args){
        Sieve mySieve = new Sieve(100);
        mySieve.print();
        System.out.println(mySieve.myArray[50]);
    }
}
CBlumey
  • 65
  • 8
  • Can you clarify the difference between "what you expect to happen" vs "what is happening?" – Krease Mar 27 '15 at 01:07
  • 1
    Just as a side note but if SieveEm() is a method then you should start it with a lowercase. – EricF Mar 27 '15 at 01:15

1 Answers1

2

I will go method by method for anything that has issues because there seems to be a bit that needs correcting.

For public void deleteStuff(int current), this int increment= current; current+=increment; seems rather redundant and hurtful because increment does not do anything special. Even more importantly, your while - loop will never terminate because on the first pass current = 0; and increment = 0;. If you do current++; then your while-loop WILL terminate. Also, if you while loop does progress and goes through the array, all true values will be set to false.

Onto print(). This method does not print anything because you set the entire myArray to false. Of course, because of the while-loop issue I mentioned earlier, the for-loop to print is never reached because you are stuck in a never ending while-loop. So, if you solve the while - loop issue, you set every element in the array to false so the statment below never executes.

if(this.myArray[i]) { System.out.println(i); }

If I notice anything else I will let you know.

Ungeheuer
  • 1,393
  • 3
  • 17
  • 32
  • Thank you! I know that it should just be this.myArray[50], I just got paranoid and started doing extra things to be safe! – CBlumey Mar 27 '15 at 01:35
  • @CBlumey I hate to be a bit of a jerk, or whatever word applies, but Joe Hutchinison's first answer was totally off and completely incorrect and did not even answer your question. Three minutes after I answer your question, he edited his response and it magically contained the issue that I mentioned in my answer. You do not have to select my answer as the correct one, but I ask that you do as I provided the correct answer to your question before Joe Hutchinson, and I didnt steal anyone's answer. – Ungeheuer Mar 27 '15 at 02:34
  • @CBlumey and perhaps also downvote his answer as he didnt provide an answer he discovered himself, instead he copied my solution. – Ungeheuer Mar 27 '15 at 02:35
  • I did see his first answer, although I saw his second answer before I saw yours. I'll mark yours as the correct one, as it's more in-depth anyways. I don't want to downvote because I'm not entirely sure. – CBlumey Mar 27 '15 at 02:36
  • @CBlumey thank you. you didnt have to change your selection, I just felt a bit miffed that my idea was claimed by another. I do appreciate your selecting mine as the correct answer, and I hope your program works out. If you have any other questions, you can send me a comment with a link to whatever your next question is. :) – Ungeheuer Mar 27 '15 at 02:38