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 7 of 7

Thread: Nested If Else Statement

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Nested If Else Statement

    I have an assignment that says to write a program that asks the user to enter the day of week. Using nested if...else statements, display output that tells the user about an activity on that particular day this is what I have(keep in mind i'm a rookie):
    import javax.swing.JOptionPane;
     
    public class Activity
    {
    	public static void main(String[] args)
     
    	{
     
     
     
     
    String , dayofWeek = ,outputMessageOne,outputMessageTwo,outputMessageThree,
     
    outputMessageFour,outputMessageFive,outputMessageSix,outputMessageSeven,Sunday;
     
     
     
    dayofWeek	= JOptionPane.showInputDialog("Enter day of week" );
     
     
    	if (dayofWeek == Sunday) {
     
     
     
     
     
    		outputMessageOne = "Today there is nothing going on";
     
    		JOptionPane.showMessageDialog( null,outputMessageOne );
     
    	}else if (dayofWeek == "Monday") {
     
     
    			outputMessageTwo = "Monday Night Football will be on tonight";
     
    			JOptionPane.showMessageDialog (null,outputMessageTwo);
     
     
     
     
     
    }else if (dayofWeek == "Tuesday") {
     
    				outputMessageThree = "Black and White Party tonight at Club Heat";
     
     
     
    				JOptionPane.showMessageDialog(null, outputMessageThree);
     
     
     
     
     
     
     
    		}else if (dayofWeek == "Wednesday") {
     
    					outputMessageFour = "Softball game at Lions Park";
     
     
     
    					JOptionPane.showMessageDialog(null, outputMessageFour);
     
     
     
     
     
     
    			}else if (dayofWeek == "Thursday") {
     
     
     
    outputMessageFive = "College Football will be on ESPN tonight";
     
     
     
    JOptionPane.showMessageDialog(null, outputMessageFive);
     
     
    					}else if (dayofWeek =="Friday") {
     
     
     
    outputMessageSix = " Party at Venturas Longue ";
     
     
     
    JOptionPane.showMessageDialog(null,outputMessageSix);
     
     
    				}else if (dayofWeek == "Saturday") {
     
     
     
    outputMessageSeven = "Baseball game at Wolf's Park";
     
     
     
    JOptionPane.showMessageDialog(null, outputMessageSeven);
     
     
     
    }else
     
     
     
      outputMessageEight = "Your seeing this message because you foolishly misspelled a word or did something wrong";
     
     
     
     
    JOptionPane.showMessageDialog(null, outputMessageEight);
     
     
     
    }
     
    }


    I get an error that says outputMessageEight might not have been initialized

    Another note when I add a brace after the last else, the program compiles without error but when it runs it just ask the asker for the day of the week and automatically displays outputMessageEight.


    What am I doing wrong? Can somebody help me out. I would greatly appreciate it.
    Last edited by helloworld922; October 5th, 2010 at 09:31 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Nested If Else Statement

    First off, please fix your post with the directions in my signature.

    I get an error that says outputMessageEight might not have been initialized
    Let me ask you a question: Where did you declare outputMessageEight? You are receiving this error because you are attempting to use outputMessageEight, but you have not initialized it in ALL SITUATIONS. You have initialized it one situation, your else statement. But you have not initialized it in every other situation. Since you are attempting to use it outside of your if/else statement, it needs to be initialized in all situations.

    Tell me if that makes sense.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Nested If Else Statement

    import javax.swing.JOptionPane;
     
     
     
    public class Activity
    {
    public static void main(String[] args)
     
    {
     
     
     
     
    String , dayofWeek = ,outputMessageOne,outputMessageTwo,outputMessageThree,
     
    // don't put a comma after String
     
    // you haven't declared those yet, let alone initialized them
    // Great for a Null Pointer Exception
    // do you mean that these are all Strings, your output messages
    // are you hoping to concat them?  i.e. "Today there is nothing going on.  Monday night football
    // will be on tonight.  Black and White party tonight at Club Heat. 
     
    outputMessageFour,outputMessageFive,outputMessageSix,outputMessageSeven,Sunday;
     
     
     
    dayofWeek = JOptionPane.showInputDialog("Enter day of week" );
     
     
    if (dayofWeek == Sunday) {
     
     
     
     
     
    outputMessageOne = "Today there is nothing going on";
     
    JOptionPane.showMessageDialog( null,outputMessageOne );
     
    }else if (dayofWeek == "Monday") {
     
     
    outputMessageTwo = "Monday Night Football will be on tonight";
     
    JOptionPane.showMessageDialog (null,outputMessageTwo);
     
     
     
     
     
    }else if (dayofWeek == "Tuesday") {
     
    outputMessageThree = "Black and White Party tonight at Club Heat";
     
     
     
    JOptionPane.showMessageDialog(null, outputMessageThree);
     
     
     
     
     
     
     
    }else if (dayofWeek == "Wednesday") {
     
    outputMessageFour = "Softball game at Lions Park";
     
     
     
    JOptionPane.showMessageDialog(null, outputMessageFour);
     
     
     
     
     
     
    }else if (dayofWeek == "Thursday") {
     
     
     
    outputMessageFive = "College Football will be on ESPN tonight";
     
     
     
    JOptionPane.showMessageDialog(null, outputMessageFive);
     
     
    }else if (dayofWeek =="Friday") {
     
     
     
    outputMessageSix = " Party at Venturas Longue ";
     
     
     
    JOptionPane.showMessageDialog(null,outputMessageSi x);
     
     
    }else if (dayofWeek == "Saturday") {
     
     
     
    outputMessageSeven = "Baseball game at Wolf's Park";
     
     
     
    JOptionPane.showMessageDialog(null, outputMessageSeven);
     
     
     
    }else
     
     
     
    outputMessageEight = "Your seeing this message because you foolishly misspelled a word or did something wrong";
     
     
     
     
    JOptionPane.showMessageDialog(null, outputMessageEight);
     
     
     
    }
     
    }

    How are you guys getting the white background for the code? I can only get gray.

  4. #4
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Nested If Else Statement

    Instead of typing [code][/code] type: [highlight=java][/highlight]

  5. The Following User Says Thank You to Brt93yoda For This Useful Post:

    javapenguin (October 6th, 2010)

  6. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Nested If Else Statement

    Even when I initialize it the program runs but only produces the last outputmessage no matter what day of the week I put.

    import javax.swing.JOptionPane;
     
     
     
    public class Activity
    {
        public static void main(String[] args)
     
        {
     
     
     
     
    String  dayofWeek  ,outputMessageOne,outputMessageTwo,outputMessageThree,
     
    outputMessageFour,outputMessageFive,outputMessageSix,outputMessageSeven,outputMessageEight;
     
     
     
    dayofWeek    = JOptionPane.showInputDialog("Enter day of week" );
     
     
        if (dayofWeek == "Sunday") {
     
     
     
     
     
            outputMessageOne = "Today there is nothing going on";
     
            JOptionPane.showMessageDialog( null,outputMessageOne );
     
     
     
        }else if (dayofWeek == "Monday") {
     
     
                outputMessageTwo = "Monday Night Football will be on tonight";
     
                JOptionPane.showMessageDialog (null,outputMessageTwo);
     
     
     
     
     
    }else if (dayofWeek == "Tuesday") {
     
                    outputMessageThree = "Black and White Party tonight at Club Heat";
     
     
     
                    JOptionPane.showMessageDialog(null, outputMessageThree);
     
     
     
     
     
     
     
            }else if (dayofWeek == "Wednesday") {
     
                        outputMessageFour = "Softball game at Lions Park";
     
     
     
                        JOptionPane.showMessageDialog(null, outputMessageFour);
     
     
     
     
     
     
                }else if (dayofWeek == "Thursday") {
     
     
     
    outputMessageFive = "College Football will be on ESPN tonight";
     
     
     
    JOptionPane.showMessageDialog(null, outputMessageFive);
     
     
                        }else if (dayofWeek =="Friday") {
     
     
     
    outputMessageSix = " Party at Venturas Longue ";
     
     
     
    JOptionPane.showMessageDialog(null,outputMessageSix);
     
     
                    }else if (dayofWeek == "Saturday") {
     
     
     
    outputMessageSeven = "Baseball game at Wolf's Park";
     
     
     
    JOptionPane.showMessageDialog(null, outputMessageSeven);
     
     
     
    }else
     
    outputMessageEight = "";
     
      outputMessageEight = "Your seeing this message because you foolishly misspelled a word or did something";
     
     
     
     
    JOptionPane.showMessageDialog(null, outputMessageEight);
     
     
     
    }
     
    }

  7. #6
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Nested If Else Statement

    since your last else statement isnt an if-else statement the statement will always run if the day is not Saturday.ALSO you are comparing strings with == you're actually supposed to use stringName.equals(string); to compare.

    import javax.swing.JOptionPane;
     
     
     
    public class Activity
    {
        public static void main(String[] args)
     
        {
     
     
     
     
    String  dayofWeek  ,outputMessageOne,outputMessageTwo,outputMessageThree,
     
    outputMessageFour,outputMessageFive,outputMessageSix,outputMessageSeven,outputMessageEight;
     
     
     
    dayofWeek    = JOptionPane.showInputDialog("Enter day of week" );
     
      if ("sundaymondaytuesdaywednesdaythursdayfridaysaturday".contains(dayofWeek.toLowerCase())) { //checks to see if the string entered is valid
     
        if (dayofWeek.equalsIgnoreCase("sunday")) {
     
     
     
     
     
            outputMessageOne = "Today there is nothing going on";
     
            JOptionPane.showMessageDialog( null,outputMessageOne );
     
     
     
        }else if (dayofWeek.equalsIgnoreCase("monday")) {
     
     
                outputMessageTwo = "Monday Night Football will be on tonight";
     
                JOptionPane.showMessageDialog (null,outputMessageTwo);
     
     
     
     
     
    }else if (dayofWeek.equalsIgnoreCase("tuesday")) {
     
                    outputMessageThree = "Black and White Party tonight at Club Heat";
     
     
     
                    JOptionPane.showMessageDialog(null, outputMessageThree);
     
     
     
     
     
     
     
            }else if (dayofWeek.equalsIgnoreCase("wednesday")) {
     
                        outputMessageFour = "Softball game at Lions Park";
     
     
     
                        JOptionPane.showMessageDialog(null, outputMessageFour);
     
     
     
     
     
     
                }else if (dayofWeek.equalsIgnoreCase("thursday")) {
     
     
     
    outputMessageFive = "College Football will be on ESPN tonight";
     
     
     
    JOptionPane.showMessageDialog(null, outputMessageFive);
     
     
                        }else if (dayofWeek.equalsIgnoreCase("friday")) {
     
     
     
    outputMessageSix = " Party at Venturas Longue ";
     
     
     
    JOptionPane.showMessageDialog(null,outputMessageSix);
     
     
                    }else if (dayofWeek.equalsIgnoreCase("saturday")) {
     
     
     
    outputMessageSeven = "Baseball game at Wolf's Park";
     
     
     
    JOptionPane.showMessageDialog(null, outputMessageSeven);
     
     
     
       }
    }
    else {
     
    outputMessageEight = "";
     
      outputMessageEight = "Your seeing this message because you foolishly misspelled a word or did something";
     
     
     
     
    JOptionPane.showMessageDialog(null, outputMessageEight);
     
     
     
    }
    }
    }

    Works perfectly. *shines nails on T-shirt*
    Last edited by Brt93yoda; October 5th, 2010 at 10:18 PM.

  8. The Following User Says Thank You to Brt93yoda For This Useful Post:

    jwill22 (October 6th, 2010)

  9. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Nested If Else Statement

    WOW Brt93yoda....u are like a freakin genius. I don't know what I was thinking trying to compare with ==. Dang thank you so much and too everybody else for having patience with a beginning Java Programmer.

Similar Threads

  1. do while loop with nested while question
    By johneusmc in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 6th, 2010, 04:45 PM
  2. nested loop
    By b109 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 30th, 2010, 10:05 AM
  3. Nested try blocks
    By Ahmed. in forum Member Introductions
    Replies: 2
    Last Post: April 30th, 2010, 08:12 AM
  4. Equilateral Triangle Using nested for loop
    By uchizenmaru in forum Loops & Control Statements
    Replies: 2
    Last Post: January 14th, 2010, 10:01 PM
  5. how do i draw a shape with nested loops?
    By Kilowog in forum Loops & Control Statements
    Replies: 1
    Last Post: September 25th, 2009, 12:14 AM