Hello all. For my project I have to create a snack machine with mints and cookies. So far I have gotten an array of cookies with the code below. However an interesting problem I'm having is my output:
OATMEAL cookie
LEMON cookie
null
CHOCOLATE_CHIP cookie
OATMEAL cookie
LEMON cookie
null
This is the output that I am receiving and repeats for 60 cookie elements. I have no idea how a null element snuck in there. There are only 3 values in the enum class. If anyone could help me point out the problem I would very much appreciate it. Thank you all.

package proj3;

public class Cookies {

    private Cookie[] cookies = new Cookie[60];

    public Cookies(){
        for(int i = 0; i < cookies.length; i++){
            for(CookieFlavors c : CookieFlavors.values()){
                cookies[i++] = new Cookie(c);
            }
        }
    }
    public Cookie[] getCookies(){
        return cookies;
    }
    public static void main(String args[]){
        Cookies cookies = new Cookies();
        for(Cookie i : cookies.getCookies()){
            System.out.println(i);
        }
    }
}




package proj3;

public class Cookie {

    private CookieFlavors flavor;

    public Cookie(CookieFlavors flavor){
        this.flavor = flavor;
    }
    public String toString(){
        String str = flavor + " cookie";
        return str;
    }
    /*public static void main(String[] args){
        Cookie card = new Cookie(CookieFlavors.CHOCOLATE_CHIP);
        System.out.println(card);
    }*/
}




package proj3;

public enum CookieFlavors {CHOCOLATE_CHIP, OATMEAL, LEMON}

Recommended Answers

All 6 Replies

for(int i = 0; i < cookies.length; **i++**){
    for(CookieFlavors c : CookieFlavors.values()){
        cookies[**i++**] = new Cookie(c);

You increment i twice on some passes of this loop, so some indexes get skipped and are left as null.

Thank you for the reply, I fixed it by changing the for loop to 'i = i' however I'm not sure if that is an efficient way to do it or if it is good programming practice?

It's not ideal because it's not obvious why you would do that to someone who just reads the code.
I would recommend this form for clarity..

int i = 0;
while (i < cookies.length) {
    for(CookieFlavors c : CookieFlavors.values()){
        cookies[i++] = new Cookie(c);
        ...

... but beware, you still have a bug that doesn't show up with an array size of 60, but will fail with 59 or 61 - if cookies.length is not an exact multiple of the nunmber of flavors then you will fall off the end of the cookies array in the inner loop before the outer loop gets a chance to exit, so you will need one more if test

Thank you. After reading through the project I found we are allowed to to use arraylists which should simplify things. But if you dont mind could you help me understand why every fourth element is null? If the array starts from 0 and gets incremented twice then shouldnt every other element be null?

Try debugging the code by print out the value of i every time it is changed as the loop executes.
Also print out the value of c that goes with that i value.

If the array starts from 0 and gets incremented twice then shouldnt every other element be null?

It's increnmented once on each pass of the inner loop (that's OK), plus once on each pass of the outer loop (which is where you get the null). Like Norm says, print the values to see whats happening.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.