1

I receive a few compiling errors when I try to loop through an enum items by a regular for loop, could anyone help me to solve the problem?

public class WeeklySales
{
 public enum weekDays {Monday, Tuesday, Wednesday, Thursday, Friday};
 static Scanner sc = new Scanner(System.in);

 public static void main(String[] args)
{  
 double[] sales = new double[5];
 double total = 0;
 weekDays day;      

 for(day = weekDays.Monday; day <= weekDays.Friday; day = (weekDays)(day + 1))
  {
    System.out.println("Enter the sales amount for " + day + ":");
    sales[day] = sc.nextDouble();
  }

 for(day = weekDays.Monday; day <= weekDays.Friday; day = (weekDays)(day +1))
    total += sales[day];

 System.out.println("The total sales for Monday to Friday is: " + total);
}
}
5
  • Could you provide a list of the errors you're receiving? Commented Jan 31, 2016 at 5:33
  • 1
    for(day = weekDays.Monday; day <= weekDays.Friday; day = (weekDays)(day + 1)) ... can you explain why you think that this should work? Have you tried to search for working ways to iterate over an enum? sales[day] can you explain why you think that an enum value would be a suitable index for an array? Commented Jan 31, 2016 at 5:34
  • It seems that you mistakenly believe that enums are numbers. They are not, they are object instances. The enums are however numbered by order of definition, accessible through the ordinal() method. Commented Jan 31, 2016 at 6:03
  • Thank you all guys for your time, here are the error lists: error: bad operand types for binary operator '<=' for(day = weekDays.Monday; day <= weekDays.Friday; day = (weekDays)(day + 1)) ^ Commented Feb 4, 2016 at 3:03
  • Why I do think this should work? because this code snippet worked in C++ (an exercise in our C++ class) , and when I asked my teacher about java he said it will work in java in the same manner just change the syntax and do the proper casting but It did NOT work, and some how he refused to give me a solution, and i asked for help in this site. Commented Feb 4, 2016 at 3:17

2 Answers 2

2

From the Java tutorial:

for (weekDays day : weekDays.values()) {
    System.out.println("Enter the sales amount for " + day + ":");
    sales[day.ordinal()] = sc.nextDouble();
}
Sign up to request clarification or add additional context in comments.

Comments

0

The following code works in my computers.

public class WeeklySales
{
    public enum weekDays {Monday, Tuesday, Wednesday, Thursday, Friday};
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args)
    {
        double[] sales = new double[5];
        double total = 0;
        for (weekDays day : weekDays.values()) {
            System.out.println("Enter the sales amount for " + day + ":");
            sales[day.ordinal()] = sc.nextDouble();
            total += sales[day.ordinal()];
        }
        System.out.println("The total sales for Monday to Friday is: " + total);
    }
}

2 Comments

Thank you LinXuan, I really appreciate your help , and it worked in my computer too!!
My pleasure~ @ArashMahmoudi

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.