Page 1 of 4 123 ... LastLast
Results 1 to 20 of 71
  1. #1
    MattTuck is offline Member
    Join Date
    Mar 2015
    Location
    A Place With Windows
    Posts
    71
    Rep Power
    0

    Exclamation Trouble with a few If-Else Statements and Strings

    I have a project for class due next Wednesday, and am having trouble with a few things: I have two if else statements that control the values presented in two different drop down menus. To me, it appears I'm not getting the information of of the previous drop down(there are three drop downs in all each affects the values presented in the next).

    Here is the chunk of code I'm struggling with:
    Java Code:
       
    ////////////////////////////////////////////DROP DOWNS//////////////////////////////////////////////////////////////// 
        //Creates subPanel2 with Occupancy, Room Type, Room, and Dining Time request
        JPanel subpanel = new JPanel();
        
        ///Occupancy///
        JLabel label2 = new JLabel("Please Select the Number of Occupants");
        JComboBox occupancy_list = new JComboBox(occupancy_string);
        occupancy_list.setSelectedIndex(3);
        //Adds Listener for Occupancy Drop Down
        String occupancy_selection = (String) occupancy_list.getSelectedItem();
        occupancy = Integer.parseInt(occupancy_selection);
        
        ///Room Type///
        JLabel label3 = new JLabel("Please Select a Room Style");
        //Sets Conditions for what Appears in Room Style DropDown
        if(occupancy>2)
        {
          roomstyle=(roomstyle2);
        }
        else
        {
          roomstyle=(roomstyle1);
        }
        //Creates RoomStyle Drop Down
        JComboBox room_type = new JComboBox(roomstyle);
        roomtype_string=(String) room_type.getSelectedItem();
     
        
        ///Room Selection///
        JLabel label4 = new JLabel("Please Select a Room");
        if(roomtype_string=="Cabin")
        {
        room_number=cabin_string;
        }
        else
        {
        room_number=suite_string;
        }
        //Creates RoomNumber Drop Down
        JComboBox room_list = new JComboBox(room_number);
        roomselected = (String) room_list.getSelectedItem();
        
        //Dining
        JLabel label5 = new JLabel("Please Select a Dining Time");
        JComboBox dining_list = new JComboBox(dining_string);
        dining_list.setSelectedIndex(1);
        
        
        //Placing Drop Down and Labels into a Sub Panel
        subpanel.add(label2);
        subpanel.add(occupancy_list); //Number of Occupants
        subpanel.add(label3);
        subpanel.add(room_type); //Style of Room
        subpanel.add(label4);
        subpanel.add(room_list); //Room Number
        subpanel.add(label5);
        subpanel.add(dining_list); //Dining Time
        
        //Panel with Drop Downs
        JPanel panel3 = new JPanel();
        panel3.add(subpanel, BorderLayout.SOUTH);//Drop Downs
        
    ///////////////////////////////////////////////////END////////////////////////////////////////////////////////////////


    Here is my code in full, thus far:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    
    
    public class Cruise_Booking
    {
      public static void main(String[] args)
      {
        String[] occupancy_string = {"4", "3", "2", "1"};
        int occupancy;
        
        String[] roomstyle1 ={"Cabin", "Suite"};
        String[] roomstyle2 ={"Suite"};
        String[] roomstyle;
        //int a;
        String roomtype_string;
        
        String[] cabin_string = {"11-1","11-2","11-3","11-4","11-5","11-6","11-7","11-8","11-9","11-10"};
        String[] suite_string = {"11-S1","11-S2"};
        String[] room_number;
        //int b;
        String roomselected;
        
        String[] dining_string = {"8:45pm", "6:30pm"};
        Boolean person_check=false;
        
        /////Occupancy, Room Style, and Room Number Listener
          
        JFrame frame = new JFrame("Cruise Booking"); //Creates JFrame labled Cruise Booking- frame is labeld "frame"
        frame.setPreferredSize(new Dimension(1300, 500));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Close the Frame when exiting
        Container content = frame.getContentPane();
    
        
    ////////////////////////////////////////////Personal Info/////////////////////////////////////////////////////////////
        
        //Creates subPanel1 with name request
        JPanel person_panel1 = new JPanel();
        JTextField name1 = new JTextField(25); //constructs box for first name
        name1.setText("First Name");
        JTextField name2 = new JTextField(25); //constructs box for last name
        name2.setText("Last Name");
        person_panel1.add(name1);
        person_panel1.add(name2);
        
        //Creates subPanel2 with City and State request
        JTextField citylabel = new JTextField(25); //constructs box for first name
        citylabel.setText("City");
        JTextField statelabel = new JTextField(25); //constructs box for last name
        statelabel.setText("State");
        person_panel1.add(citylabel);
        person_panel1.add(statelabel);
        
        //Combines Name, City, and State into one Panel   
        JPanel panel1 = new JPanel();
        //Inserts subPanel(s) into container labeled "content" and then JFrame
        panel1.add(person_panel1);
        
    //////////////////////////////////////////////////END//////////////////////////////////////////////////////////////////
        
    //////////////////////////////////////////SHIP/////////////////////////////////////////////////////////
          
        //Creates subPanel1 with Ship.gif
        JPanel panel2 = new JPanel();
        ImageIcon ship = new ImageIcon("/Users/Tuck/Documents/Docs/Java/Cruise_Booking/src/Ship.gif");//Needs to be changed for project.
        JLabel label1;
        label1 = new JLabel("", ship, SwingConstants.CENTER);
        panel2.add(label1);
        
        
    ////////////////////////////////////////////////END////////////////////////////////////////////////////////////////////    
       
    ////////////////////////////////////////////DROP DOWNS//////////////////////////////////////////////////////////////// 
        //Creates subPanel2 with Occupancy, Room Type, Room, and Dining Time request
        JPanel subpanel = new JPanel();
        
        ///Occupancy///
        JLabel label2 = new JLabel("Please Select the Number of Occupants");
        JComboBox occupancy_list = new JComboBox(occupancy_string);
        occupancy_list.setSelectedIndex(3);
        //Adds Listener for Occupancy Drop Down
        String occupancy_selection = (String) occupancy_list.getSelectedItem();
        occupancy = Integer.parseInt(occupancy_selection);
        
        ///Room Type///
        JLabel label3 = new JLabel("Please Select a Room Style");
        //Sets Conditions for what Appears in Room Style DropDown
        if(occupancy>2)
        {
          roomstyle=(roomstyle2);
        }
        else
        {
          roomstyle=(roomstyle1);
        }
        //Creates RoomStyle Drop Down
        JComboBox room_type = new JComboBox(roomstyle);
        roomtype_string=(String) room_type.getSelectedItem();
     
        
        ///Room Selection///
        JLabel label4 = new JLabel("Please Select a Room");
        if(roomtype_string=="Cabin")
        {
        room_number=cabin_string;
        }
        else
        {
        room_number=suite_string;
        }
        //Creates RoomNumber Drop Down
        JComboBox room_list = new JComboBox(room_number);
        roomselected = (String) room_list.getSelectedItem();
        
        //Dining
        JLabel label5 = new JLabel("Please Select a Dining Time");
        JComboBox dining_list = new JComboBox(dining_string);
        dining_list.setSelectedIndex(1);
        
        
        //Placing Drop Down and Labels into a Sub Panel
        subpanel.add(label2);
        subpanel.add(occupancy_list); //Number of Occupants
        subpanel.add(label3);
        subpanel.add(room_type); //Style of Room
        subpanel.add(label4);
        subpanel.add(room_list); //Room Number
        subpanel.add(label5);
        subpanel.add(dining_list); //Dining Time
        
        //Panel with Drop Downs
        JPanel panel3 = new JPanel();
        panel3.add(subpanel, BorderLayout.SOUTH);//Drop Downs
        
    ///////////////////////////////////////////////////END////////////////////////////////////////////////////////////////    
        
    ////////////////////////////////////////////////Build Frame//////////////////////////////////////////////////////////    
        
        //Builds First Frame
        content.add(panel1, BorderLayout.NORTH);
        content.add(panel2, BorderLayout.CENTER);
        content.add(panel3, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    
        //creates and sets name and home string variables
        String name = name1.getText() + " "+ name2.getText(); //creates and sets name equal to users input for first and last name
        String home = citylabel.getText() + ", "+ statelabel.getText(); //creates and sets name equal to users input for city and state
        String person = name+" "+home; //creates and sets person equal to name and home
        if(person.equals("First Name Last Name City, State"))
        person_check=false;
        else
        person_check=true;
        
        
        
        //
        //
        //
        //
        //System.out.println(person+": Room "+roomselected+" "+person_check);
        //
        //
        //
    
      }
    }
    Note:The "//System.out.println(person+": Room "+roomselected+" "+person_check);" chunk of code is there as a check method.





    My next task is to add an enter button and write the frame into a while loop, while (enter = false || person_check = false), so it changes the frame when conditions conditions fail.

    In the end, the point of the program will be to imitate a cruise booking page. To take in the users name, city, and state, offer them a list of rooms to select from, ask for number of occupants and preferred dining, and have the user select a cruises and 5 excursions while on their cruise. Then to print out their name, city, state, room, number of occupants, the cruise and list of excursion, the port the cruise will be leaving from, and the total cost of the cruise.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Trouble with a few If-Else Statements and Strings

    One problem is the use of == to compare Strings. Objects like Strings should use the equals() method to compare their contents.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    MattTuck is offline Member
    Join Date
    Mar 2015
    Location
    A Place With Windows
    Posts
    71
    Rep Power
    0

    Default Re: Trouble with a few If-Else Statements and Strings

    So, "if(roomtype_string==("Cabin"))" to "if(roomtype_string.equals("Cabin"))"... check. Still having a problem when I run the code.

    Say I select "4" occupants in my drop down, it should then limit the next drop down to a choice of just "suite." It's not. Nothing changes if I select something in the drop down. Suggestions?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Trouble with a few If-Else Statements and Strings

    In what order do the events happen?
    When are each of the lists filled?
    When does the user make a selection?

    If you can't tell, add some println statements that prints a message when each event happens.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    MattTuck is offline Member
    Join Date
    Mar 2015
    Location
    A Place With Windows
    Posts
    71
    Rep Power
    0

    Default Re: Trouble with a few If-Else Statements and Strings

    Sounds like you're suggesting I'm missing an event listener?
    I thought that's what the ".getSelectedItem()" method was for?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Trouble with a few If-Else Statements and Strings

    Yes, the getSelectedItem() method returns what is selected at the time it is called. Using a listener would have it be called after a user makes a selection.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    MattTuck is offline Member
    Join Date
    Mar 2015
    Location
    A Place With Windows
    Posts
    71
    Rep Power
    0

    Default Re: Trouble with a few If-Else Statements and Strings

    So I guess then, my question should be, how do I build a listener?

  8. #8
    MattTuck is offline Member
    Join Date
    Mar 2015
    Location
    A Place With Windows
    Posts
    71
    Rep Power
    0

    Default Re: Trouble with a few If-Else Statements and Strings

    It's my assumption for my first drop down, something like this should be suffice:
    Java Code:
        ///Occupancy///
        JLabel label2 = new JLabel("Please Select the Number of Occupants");
        JComboBox occupancy_list = new JComboBox(occupancy_string);
        occupancy_list.setSelectedIndex(3);
        //Adds Listener for Occupancy Drop Down
        Occupancy_Listener occupancy = new Occupancy_Listener();
    
         
        ///Room Type///
        JLabel label3 = new JLabel("Please Select a Room Style");
        //Sets Conditions for what Appears in Room Style DropDown
        if(occupancy>2)
        {
          roomstyle=(roomstyle2);
        }
    .....
    .....
    .....
      }
      
      private class Occupancy_Listener implements ActionListener 
      {
         public void actionPerformed(ActionEvent event)
         {
          JComboBox cb = (JComboBox)event.getSource(); //grab the user selection
          String occupancy_selection = (String) cb.getSelectedItem();
          int occupancy = Integer.parseInt(occupancy_selection);
         }
      }

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Trouble with a few If-Else Statements and Strings

    Does it work?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    MattTuck is offline Member
    Join Date
    Mar 2015
    Location
    A Place With Windows
    Posts
    71
    Rep Power
    0

    Default Re: Trouble with a few If-Else Statements and Strings

    no, that was meant more as a request to clarify what I'm doing wrong

  11. #11
    MattTuck is offline Member
    Join Date
    Mar 2015
    Location
    A Place With Windows
    Posts
    71
    Rep Power
    0

    Default Re: Trouble with a few If-Else Statements and Strings

    To clarify, for this particular part, I am trying to retrieve the users selection in the "occupancy_list" drop down. Turn the value into an int, "occupancy", and then use it to limit the values presented in the "room_type" drop down. So if, the value passed into "occupancy" is greater than 2, the only possible option in "room_type" is "suite," otherwise the option is "suite or cabin."

    I feel like, if I can figure out how to build that listener, or have it explained to me, I will be able to do the same for the other drop downs, and expand on it to work for my text boxes as well.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Trouble with a few If-Else Statements and Strings

    Have you discovered the Oracle tutorials? How to Use Combo Boxes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    Also, learn to use conventional (and meaningful) variable names: Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    MattTuck is offline Member
    Join Date
    Mar 2015
    Location
    A Place With Windows
    Posts
    71
    Rep Power
    0

    Default Re: Trouble with a few If-Else Statements and Strings

    I've been looking at the tutorial ALL DAY... And haven't made much headroom...

    This is my most recent draft of the code, now if I select something everything in the drop down disappears:
    Java Code:
    ///Occupancy///
        JLabel label2 = new JLabel("Please Select the Number of Occupants");
        JComboBox occupancy_list = new JComboBox(occupancy_string);
        occupancy_list.setSelectedIndex(3);
        //Adds Listener for Occupancy Drop Down
        occupancy_list.addActionListener(occupancy_list);
        String occupancy_selection = (String) occupancy_list.getSelectedItem();
        int occupancy = Integer.parseInt(occupancy_selection);
    ...
    ...
    ...
      private class Listener implements ActionListener 
      {
         public void actionPerformed(ActionEvent event)
         {
          JComboBox cb = (JComboBox)event.getSource(); //grab the user selection
          String selection = (String) cb.getSelectedItem();
         }

  14. #14
    MattTuck is offline Member
    Join Date
    Mar 2015
    Location
    A Place With Windows
    Posts
    71
    Rep Power
    0

    Default Re: Trouble with a few If-Else Statements and Strings

    What am I doing wrong? What do I need to fix, and how? The tutorial isn't helping. I honestly just don't see what's wrong here.

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Trouble with a few If-Else Statements and Strings

    Quote Originally Posted by MattTuck View Post
    Java Code:
        //Adds Listener for Occupancy Drop Down
        occupancy_list.addActionListener(occupancy_list);
    Go back to the examples in the tutorial. Does this look correct to you?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  16. #16
    MattTuck is offline Member
    Join Date
    Mar 2015
    Location
    A Place With Windows
    Posts
    71
    Rep Power
    0

    Default Re: Trouble with a few If-Else Statements and Strings

    It sounds to me, like you're suggesting I change it to:
    Java Code:
    occupancy_list.addActionListener(this);
    But when I do, Eclipse spits out errors and won't run...

    It keeps saying, "can not use this in a static context"
    Last edited by MattTuck; 03-28-2015 at 04:10 PM.

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Trouble with a few If-Else Statements and Strings

    What is the purpose of the Listener class that you have defined? It looks like it would do some of what you want. Make an instance of it and pass it in the add listener method.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    MattTuck is offline Member
    Join Date
    Mar 2015
    Location
    A Place With Windows
    Posts
    71
    Rep Power
    0

    Default Re: Trouble with a few If-Else Statements and Strings

    Not sure what you mean....

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Trouble with a few If-Else Statements and Strings

    Which part don't you understand?
    Make an instance of the class
    call the add listener method with that instance
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    MattTuck is offline Member
    Join Date
    Mar 2015
    Location
    A Place With Windows
    Posts
    71
    Rep Power
    0

    Default Re: Trouble with a few If-Else Statements and Strings

    I thought that I already did...
    Java Code:
        JComboBox occupancy_list = new JComboBox(occupancy_string);
        occupancy_list.setSelectedIndex(3);
        occupancy_list.addActionListener(occupancy_list);//Adds Listener for Occupancy Drop Down
    ...
    ...
    ...
          
      public void actionPerformed(ActionEvent event)
      {
          JComboBox cb = (JComboBox)event.getSource(); //grab the user selection
          //String selection = (String) cb.getSelectedItem();
      }

Page 1 of 4 123 ... LastLast

Similar Threads

  1. If statements with Strings
    By SabuLuvu in forum New To Java
    Replies: 4
    Last Post: 05-09-2013, 04:41 PM
  2. If Statements or Something Better?
    By collegeconfused in forum New To Java
    Replies: 1
    Last Post: 05-05-2013, 10:28 AM
  3. Replies: 4
    Last Post: 10-12-2011, 07:17 AM
  4. if else statements
    By sweetpea123 in forum New To Java
    Replies: 4
    Last Post: 04-12-2010, 07:02 PM
  5. If statements, input, and strings
    By evolvepwnz in forum New To Java
    Replies: 16
    Last Post: 10-22-2009, 09:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •