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}