0

I'm trying to get all of the indexes of a Boolean array to be printed out where its element is true. The end goal is to be able to find a prime number of the indexes (where I change each index number that isn't prime to false in the array) then print out only what is left of the prime numbers of the indexes of the array.

The very first step I'm just trying to do is at least to get some integer index to print out, but nothing seems to be working and I don't know what is wrong.

public class PriNum{
    private boolean[] array;

    public PriNum(int max){
        if (max > 2){ //I don't have any problems with this if statement
            throw new IllegalArgumentException();
        }
        else{
            array = new boolean[max];

            for(int i = 0; i < max; i++){
                if(i == 0 || i == 1){ //Automatically makes 0 and 1 false 
                                      //because they are not prime
                    array[i] = false;
                }
                else{
                    array[i] = true;
                }
            }
            toString(); //I know for sure the code gets to here 
                        //because it prints out a string I have
                        // there, but not the index
        }
    }

    public String toString(){
        String s = "test"; //this only prints test so I can see if 
                           //the code gets here, otherwise it would just be ""

        for (int i = 0; i < array.length; i++){
            if(array[i] == true){
                s = s + i; //Initially I tried to have the indexes returned
                         //to be printed and separated by a comma,
                         //but nothing comes out at all, save for "test"
             }
         }

        return s;
    }
}

EDIT: Included is the driver class that's requesting the print of the class PriNum

class Driver{
    public static void main(String [] args){
        PriNum theprime = null;

        try{
            theprime = new PriNum(50);
        }
        catch (IllegalArgumentException oops){
            System.out.println("Max must be at least 2.");
        }

        System.out.println(theprime);
    }
}
7
  • private boolean[] array; is never intialized. What is numbers ? I think this code would not even compile. Commented Oct 16, 2018 at 1:55
  • What is max, and what index do you expect to be true. I note you have a typo (missing } before your else in that first loop). Also, how are you printing this? Calling toString() and doing nothing with the result doesn't accomplish much. Commented Oct 16, 2018 at 1:55
  • @ElliottFrisch max is supposed to be the maximum size of the array, and it is being printed by a driver class outside of this class (which I can also include if that's necessary), and thanks for catching the typo; it's fixed now Commented Oct 16, 2018 at 2:00
  • @ScaryWombat whoops, the numbers was an error on my part, it was supposed to be array for me to tell what exactly I'm working with. Commented Oct 16, 2018 at 2:01
  • 1
    after reading your code, I guess you want if (max < 2) throw... not if (max > 2) throw... , don't you? Commented Oct 16, 2018 at 2:32

2 Answers 2

1

I tried running this, and the first change that needs to happen is to set this argument:

if(max < 2)

Then, if I'm reading this correctly: 0 and 1 are false. Every index after that is true. The output is fine as I see it. Just all the numbers crunched as a continuous list.

To get a better output, put a space between indexes:

if(array[i] == true){
    s = s + " " + i;
}

You may even just output to screen directly as

if(array[i])
 System.out.print( i );
Sign up to request clarification or add additional context in comments.

Comments

0

numbers is initialized without declaration, array is declared but not initialized anywhere in your code. You have also a syntax error after array[i] = true, should be curly brace...

2 Comments

That was an error on my part; numbers was supposed to be array. It's fixed now along with the syntax error!
I think your code would always throw the IllegalArgumentException cos argument is always more than 2 (50), means toString() doesn't get called at all or am I missing something?

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.