lundi 31 octobre 2016

Checking for enumeration equality is not working

I am having trouble getting the program to print either "Fruit", "Drink", or "Unknown" (followed by a newline) depending on the value of userItem. I have also tried switching (userItem == x) to (GroceryItem == x) and still no luck. I am stuck understanding on what my error is.

My Code:

import java.util.Scanner;

public class GrocerySorter {
   public enum GroceryItem {GR_APPLES, GR_BANANAS, GR_JUICE, GR_WATER};
      public static void main (String [] args) {

      GroceryItem userItem = GroceryItem.GR_APPLES;

      if (userItem == GR_APPLES) {
          System.out.println("Fruit"); 
      }
      else if (userItem == GR_BANANAS) {
         System.out.println("Fruit"); 
      }
      else if (userItem == GR_JUICE) {
        System.out.println("Drink"); 
      }
      else if (userItem == GR_WATER) {
        System.out.println("Drink"); 
      }
      else {
        System.out.println("Unknown");
      }

      return;
   }
}

Errors:

GrocerySorter.java:9: cannot find symbol
symbol  : variable GR_APPLES
location: class GrocerySorter
      if (userItem == GR_APPLES) {
                      ^
GrocerySorter.java:12: cannot find symbol
symbol  : variable GR_BANANAS
location: class GrocerySorter
      else if (userItem == GR_BANANAS) {
                           ^
GrocerySorter.java:15: cannot find symbol
symbol  : variable GR_JUICE
location: class GrocerySorter
      else if (userItem == GR_JUICE) {
                           ^
GrocerySorter.java:18: cannot find symbol
symbol  : variable GR_WATER
location: class GrocerySorter
      else if (userItem == GR_WATER) {
                           ^
4 errors

1 commentaire:

  1. if (userItem == GroceryItem.GR_APPLES){
    System.out.println("Fruit");
    }
    else if (userItem == GroceryItem.GR_BANANAS) {
    System.out.println("Fruit");
    }
    else if (userItem == GroceryItem.GR_JUICE) {
    System.out.println("Drink");
    }
    else if (userItem == GroceryItem.GR_WATER) {
    System.out.println("Drink");
    }
    else {
    System.out.println("Unknown");
    }

    RépondreSupprimer