-1

Hi I can't get my if statement to work.

if (foods.iterator().next() != Meal.LUNCH)

In this line,I get an error that says Operator '!=' cannot be applied to 'comp1110.exam.Q1MealPlan.Food', 'comp1110.exam.Q1MealPlan.Meal'

 public static Set<List<Food>> getAllMealPlans(Set<Food> foods, int numMeals) {
        Set<List<Food>> set = new HashSet<>();
        List<Food> aList = new ArrayList<Food>();
        System.out.println(aList);
        System.out.println(foods.iterator().next().meal);
        if (foods.iterator().next().meal == Meal.BREAKFAST){
            aList.add(foods.iterator().next());
            set.remove(foods.iterator().next());
            if (foods.iterator().next() != Meal.LUNCH){

            }
        }
Mikxas
  • 33
  • 3
  • In all likelihood, based on the `if` statement above the problematic line, you probably meant `foods.iterator().next().meal` rather than `foods.iterator().next()`. Not putting this as an answer because this is a bit of a guess. The question could use some more details. – micpap25 Nov 08 '20 at 07:29
  • Also going to add, use `.equals()` instead of boolean operators when working with objects. – micpap25 Nov 08 '20 at 07:30
  • 1
    @micpap25 you are correct, but in this specific case (for enums) it makes no difference https://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals – JavaMan Nov 08 '20 at 07:43

2 Answers2

1

I've created a Test class which should help you with your question, you've got several problems in your code.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

    public class Test {

    public static void main(String[] args) {
        new Test().test();
    }

    void test() {
        Set<Food> foods = new HashSet<>();
        foods.add(new Food("a", Meal.BREAKFAST));
        foods.add(new Food("b", Meal.BREAKFAST));
        foods.add(new Food("c", Meal.LUNCH));

        Set<List<Food>> mealPlans = getAllMealPlans(foods, 0);
        mealPlans.forEach(f -> System.out.println(Arrays.toString(f.toArray())));

        System.out.println("--------------------");

        //I think what you might want is a Map from Meal to List of Foods
        Map<Meal, List<Food>> mealPlans2 = getAllMealPlansMap(foods);
        mealPlans2.entrySet().forEach(f ->{
            System.out.println("Meal: "+f.getKey());
            System.out.println(Arrays.toString(f.getValue().toArray()));
            System.out.println("");
        });
    }

    public static Set<List<Food>> getAllMealPlans(Set<Food> foods, int numMeals) { //numMeals unused?
        Set<List<Food>> set = new HashSet<>();

        Iterator<Food> iterator = foods.iterator(); // only instantiate the iterator once(!)
        while (iterator.hasNext()) {
            Food food = iterator.next(); //only call .next once(!)
            List<Food> aList = new ArrayList<Food>();
            if (food.meal == Meal.BREAKFAST) {
                //set.remove(foods.iterator().next()); //why remove it from the set?.. and by foods.iterator() you create a NEW Iterator which is starting at index 0 again .. and check that you are calling .remove with the correct type
                aList.add(food);
                if (food.meal != Meal.LUNCH) {
                    //
                }
            }

            if(!aList.isEmpty()) {//only add the list if its not empty
                set.add(aList);
            }
        }
        return set;
    }


    public static Map<Meal,List<Food>> getAllMealPlansMap(Set<Food> foods) {
        return foods.stream().collect(Collectors.groupingBy(f -> f.meal));
    }


    enum Meal {
        LUNCH, BREAKFAST
    }

    class Food {
        String name;
        Meal meal;

        public Food(String name, Meal meal) {
            super();
            this.name = name;
            this.meal = meal;
        }

        @Override
        public String toString() {
            return name + " " + meal;
        }
    }
    }
JavaMan
  • 1,142
  • 12
  • 22
0

You forgot to add .meal

here you have it: if(foods.iterator().next().meal == Meal.BREAKFAST)

here it's misssing: if (foods.iterator().next() != Meal.LUNCH)

So just change it to if(foods.iterator().next().meal != Meal.LUNCH)

InUser
  • 1,138
  • 15
  • 22