Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: for-each loop and a switch statement

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default for-each loop and a switch statement

    Hey I'm trying to Write a class called MonthTest.java that uses the enum Month and outputs the
    following:
    January has 30 days.
    February has 28 days.
    March 31 days.

    December 31 days.

    I'm trying to have MonthTest.java use a for-each loop and a switch statement.

     
    public enum Month
    {
    	JANUARY ,
    	FEBRUARY,
    	MARCH,
    	APRIL,
    	MAY,
    	JUNE,
    	JULY,
    	AUGUST,
    	SEPTEMBER,
    	OCTOBER,
    	NOVEMBER,
    	DECEMBER
     
    }
     
    public class MonthTest {
    	 Month month;
     
     
        public void Month(Month month) {
            this.month = month;
        }
     
    		    public void tellItLikeItIs() {
    		        switch (month) {
    		            case JANUARY:
    		                System.out.println("January has 30 days.");
    		                break;
    		            case FEBRUARY:
    		                System.out.println("February has 28 days.");
    		                break;
    		            case MARCH:
    		                System.out.println("March has 31 days.");
    		                break;
    		            case APRIL:
    		                System.out.println("April has 30 days.");
    		                break;
    		            case MAY:
    		                System.out.println("May has 31 days.");
    		                break;
    		            case JUNE:
    		                System.out.println("June has 30 days.");
    		                break;
    		            case JULY:
    		                System.out.println("July has 31 days.");
    		                break;
    		            case AUGUST:
    		                System.out.println("August has 31 days.");
    		                break;
    		            case SEPTEMBER:
    		                System.out.println("September has 30 days.");
    		                break;
    		            case OCTOBER:
    		                System.out.println("October has 31 days.");
    		                break;
    		            case NOVEMBER:
    		                System.out.println("November has 30 days.");
    		                break;
    		            case DECEMBER:
    		                System.out.println("December has 31 days.");
    		                break;
    	                default:
    	                System.out.println("error.");
    		        }
    		    }
    	            public static void main(String[] args) {
     
    	            	for (Month d : Month.values()){
    	        			System.out.println(d);
    	        			}	            	
     
    	        }
    	}

    What I'm trying to figure out is how do I loop through the enum with the switch statement and output it through the for loop


  2. #2
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: for-each loop and a switch statement

    What output are you expecting? What is your code outputting? Is your code compilable?

    Wrap your code in tags

    [C0DE=java]
    <code>
    [/CODE]

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: for-each loop and a switch statement

    Do you have a problem? Please explain and ask some questions about it.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: for-each loop and a switch statement

    What I'm trying to figure out is how do I loop through the enum with the switch statement and output it through the for loop

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: for-each loop and a switch statement

    Add an arg to the method and call the method in the loop passing it an arg.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: for-each loop and a switch statement

    You have a public enum class named Month. Its members are constants that represent the months of the year.

    You have a public class named MonthTest.

    The MonthTest class has
    1. An instance variable of type Month named month.

    2. A month setter method named Month(). I think that "Month" is a terrible choice for a method in this class, since we mere humans might get confused by the existence of an enum class with the same name. Since this method's purpose is to give a value to the instance variable month, why not call it setMonth() or some such thing?

    3. A method named tellItLikeItIs() that prints out the information about the value of the month instance variable.

    4. A main method that has a loop that lets the variable d take on values from the Month enum class.



    So, here's one way to complete the assignment using the classes and their variables and methods that you have already defined.

    1. In main(), create an MonthTest object. Call the object mt or some such thing. (Maybe you can think of a name that is more descriptive and less cryptic.)

    2. Inside the loop that lets d go through the values of the Month enum:
      • Use the month setter method of the mt object to set its instance variable month to the value of d
      • Use the tellItLikeItIs() method of the mt object to print the information.



    Cheers!

    Z

Similar Threads

  1. How do I loop a switch Statement?
    By Arkeshen in forum Java Theory & Questions
    Replies: 10
    Last Post: August 2nd, 2018, 07:47 AM
  2. Replies: 9
    Last Post: February 24th, 2013, 06:51 PM
  3. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  4. Do you add objects to an array when using switch statement with a For loop?
    By TheWhopper858 in forum Collections and Generics
    Replies: 2
    Last Post: November 13th, 2011, 01:28 PM
  5. Switch statement inside a for loop need help.
    By TheWhopper858 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 12th, 2011, 11:50 PM