Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy Compiler issue with else and if

    I am a student. I have eliminated all errors except this one.

    Reservations.java:108: 'else' without 'if'
    else
    ^
    1 error

    Tool completed with exit code 1
    I have both if and else there. I have gone through the code looking for another problem, but there are none. I am in a time crunch with this chapter ending tomorrow night.

    If you could point me in the right direction it would be fantastic. Because I am in a bind, and do not know where else to look.

    here is the area that the compiler is referring to.

    PHP Code:
    public void actionPerformed(ActionEvent e)
        {
            if (
    hidden.getState());
            {
                
    JOptionPane.showMessageDialog(null,"You must select Nonsmoking or Smoking.","Error",JOptionPane.ERROR_MESSAGE);
            }
            else
            {
                
    int available room.bookRoom(Smoking.getState());

                if (
    available 0//room is available
                
    {
                    
    roomDisplay[available].setBackground(lightRed);//diplay room as occupied
                    
    roomDisplay[available].setText(
                                                    
    roomDisplay[available].getText() +
                                                    
    "\n" +
                                                    
    nameField.getText() +
                                                    
    " " +
                                                    
    phoneField.getText() +
                                                    
    "\nparty of " +
                                                    
    numberOfGuests.getSelectedItem()
                                                    );
    //display info in room
                    
    clearFields();
                }
                else 
    //rooms is not available
                
    {
                    if (
    Smoking.getState())
                        
    JOptionPane.showMessageDialog(null,"Smoking is Full","Error",JOptionPane.INFORMATION_MESSAGE);
                    else
                        
    JOptionPane.shoeMessageDialog(null,"Nonsmoking is Full","Error",JOptionPane.INFORMATION_MESSAGE);
                    
    hidden.setState(true);
                }
    //end of else block that checks the available room number
            
    }//end of else block that checks the state of the hidden option button
        
    }//end of the actionperformed() method 

    Here is the full code
    PHP Code:
    import javax.swing.JOptionPane;
    import java.awt.*;
    import java.awt.event.*;

    public class 
    Reservations extends Frame implements ActionListener
    {
        
    color lightRed = new color(2559090);
        
    color lightGreen = new color(14021540);

        
    Rooms room = new Rooms(53);

        
    Panel roomPanel = new Panel();
            
    TextArea roomDisplay[] = new TextArea[9];

        
    Panel buttonPanel = new Panel();
            
    Button bookButton = new button("Book Room");

        
    Panel inputPanel = new Panel();
            
    Label custNameLabel = new Label("Name");
            
    TextField nameField = new TextField(15);
            
    Label custPhoneLabel = new Label("Phone Number");
            
    TextField phoneField = new TextField(15);
            
    Label numLabel = new Label("Number in Party");
            
    Choice numberOfGuests = new Choice();
            
    CheckboxGroup optios = new CheckboxGroup();
                
    Checkbox nonSmoking = new Checkbox("Nonsmoking",false,options);
                
    Checkbox Smoking = new Checkbox("Smoking",false,options);
                
    Checkbox hidden = new CheckBox("",true,options);

        public 
    Reservations()
        {
            
    //set Layouts for frame and three panels
            
    this.setLayout(new BorderLayout());
                
    roomPanel.setLayout(new GridLayout(2,4,10,10));
                
    buttonPanel.setLayout(new FlowLayout());
                
    inputPanel.setLayout(new FlowLayout());

            
    //add components to room panel
            
    for (inti=1i<9i++)
            {
                
    roomDisplay[i] = new TextArea(null,3,5,3);
                if(
    i<6)
                    
    roomDisplay[i].setText("Room " "NonSmoking");
                else
                    
    roomDisplay[i].setText("Room " "Smoking");
                
    roomDisplay[i].SetEditable(false);
                
    roomDisplay[i].setBackground(lightGreen);
                
    roomDisplay.add(roomDisplay[i]);
            }

            
    //add components to button panel
            
    buttonPanel.add(bookButton);

            
    //add components to input panel
            
    inputPanel.add(custNameLabel);
            
    inputPanel.add(NameField);
            
    inputPanel.add(custPhoneLabel);
            
    inputPanel.add(PhoneField);
            
    inputPanel.add(numLabel);
            
    inputPanel.add(numberOfGuests);
                for(
    int i 8i<=20i++)
                    
    numberOfGuests.add(String.valueOf(i));
            
    inputPanel.add(nonSmoking);
            
    inputPanel.add(Smoking);

            
    //adds panel to Frame
            
    add(buttonPanelBorderLayout.South);
            
    add(inputPanelBorderLayout.Center);
            
    add(roomPanelBorderLayout.North);

            
    bookButton.addActionListener(this);

            
    //override the windowClosing() method will allow the user to click the close button
            
    addWindowListener(
                new 
    WindowAdapter()
                {
                    public 
    void windowClosing(WindowEvent e)
                    {
                        
    System.exit(0);
                    }
                }
                );
        }
    //end of constructor method

        
    public static void main(String[] args)
        {
            
    Reservations f = new Reservations();
            
    f.setBounds(200,200,600,300);
            
    f.setTitle("Reserve a Party Room");
            
    f.setVisible(true);
        }
    //end of main

        
    public void actionPerformed(ActionEvent e)
        {
            if (
    hidden.getState());
            {
                
    JOptionPane.showMessageDialog(null,"You must select Nonsmoking or Smoking.","Error",JOptionPane.ERROR_MESSAGE);
            }
            else
            {
                
    int available room.bookRoom(Smoking.getState());

                if (
    available 0//room is available
                
    {
                    
    roomDisplay[available].setBackground(lightRed);//diplay room as occupied
                    
    roomDisplay[available].setText(
                                                    
    roomDisplay[available].getText() +
                                                    
    "\n" +
                                                    
    nameField.getText() +
                                                    
    " " +
                                                    
    phoneField.getText() +
                                                    
    "\nparty of " +
                                                    
    numberOfGuests.getSelectedItem()
                                                    );
    //display info in room
                    
    clearFields();
                }
                else 
    //rooms is not available
                
    {
                    if (
    Smoking.getState())
                        
    JOptionPane.showMessageDialog(null,"Smoking is Full","Error",JOptionPane.INFORMATION_MESSAGE);
                    else
                        
    JOptionPane.shoeMessageDialog(null,"Nonsmoking is Full","Error",JOptionPane.INFORMATION_MESSAGE);
                    
    hidden.setState(true);
                }
    //end of else block that checks the available room number
            
    }//end of else block that checks the state of the hidden option button
        
    }//end of the actionperformed() method

        //reset the text fields and choice components
        
    void cleadFields()
        {
            
    nameField.setText("");
            
    phoneField.setText("");
            
    numberOfGuests.setText(0);
            
    nameField.requestFocus();
            
    hidden.setState(true);
        }
    //end of clearFields() method

    }//end of reservation class 

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Ask a mod to move you over to the right forum.


    It is your responsibility to die() if necessary….. - PHP Manual

  • #3
    New Coder
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Ask a mod to move you over to the right forum.


    It is your responsibility to die() if necessary….. - PHP Manual
    Thanks.

    Can a Mod. Please move this to the correct Forums. Sorry for the confusion.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    Quote Originally Posted by bookittysdad View Post
    Can a Mod. Please move this to the correct Forums. Sorry for the confusion.
    You may wait a long time before a mod passes by and notices this. Suggest you PM one of them (suitably grovelling) - their names are at the bottom right of the main page http://www.codingforums.com/forumdis...p?s=&forumid=2

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,648 Times in 4,610 Posts
    Ready to kick yourself?

    Code:
        if (hidden.getState());
    The compiler tells the truth. By putting in that semicolon, you just made the {...} block that follows irrelevant to the if/else and, indeed, the else is now extraneous.

    By the by, you'd get the same error in JavaScript. Or C or C++ or C#.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,648 Times in 4,610 Posts
    And Philip would tell you that if you would *ALWAYS* put your left brace directly after the conditions (if/while/do/etc.) you'll never have this problem:
    Code:
        public void actionPerformed(ActionEvent e) {
            if (hidden.getState()) {
                JOptionPane.showMessageDialog(null,"You must select Nonsmoking or Smoking.","Error",JOptionPane.ERROR_MESSAGE);
            } else {
                int available = room.bookRoom(Smoking.getState());
    
                if (available > 0) { //room is available
    ... etc. ...
    And NEVER NEVER NEVER use an if that does *NOT* have { }!!!

    *** NO ****
    Code:
                if(i<6)
                    roomDisplay[i].setText("Room " + i + "NonSmoking");
                else
                    roomDisplay[i].setText("Room " + i + "Smoking");
    *** YES ***
    Code:
                if(i<6) {
                    roomDisplay[i].setText("Room " + i + "NonSmoking");
                } else {
                    roomDisplay[i].setText("Room " + i + "Smoking"); 
                }
    (That one is a hard and fast rule where I work. You get your hands slapped for omitting the {...}.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,648 Times in 4,610 Posts
    See, Philip, I'm coming around. <grin/> Though not completely.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    Quote Originally Posted by Old Pedant View Post
    See, Philip, I'm coming around. <grin/> Though not completely.
    Glad to know that you are learning, albeit slowly.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,648 Times in 4,610 Posts
    Of course, even better than
    Code:
                if(i<6) {
                    roomDisplay[i].setText("Room " + i + "NonSmoking");
                } else {
                    roomDisplay[i].setText("Room " + i + "Smoking"); 
                }
    is
    Code:
                roomDisplay[i].setText("Room " + i + ( i < 6 ? "NonSmoking" : "Smoking") );
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    Quote Originally Posted by Old Pedant View Post
    Of course, even better than
    Code:
                if(i<6) {
                    roomDisplay[i].setText("Room " + i + "NonSmoking");
                } else {
                    roomDisplay[i].setText("Room " + i + "Smoking"); 
                }
    is
    Code:
                roomDisplay[i].setText("Room " + i + ( i < 6 ? "NonSmoking" : "Smoking") );

    Yes, but ternary operators can be confusing and harder to debug, and don't really achieve anything more than the more extended code does. They do have their uses - but not very often for me. I rate clarity of code more than compactness.

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,648 Times in 4,610 Posts
    Way back in the dawn of civilization, the way I learned C was to be assigned to write a compiler for the language. (Never mind that I'd never written a compiler and never seen the language before.) This was for a Z-80 computer (8-bit processor, 64K-byte address space) and we had about 48KB of memory to work with. And, yes, we wrote the compiler itself in C and, at the end, used it to compile itself.

    I'm not sure I could have fit the code into 48KB without using ternary operators. You'd be amazed at how efficient the machine code is that they can produce compared to conventional if/else. That and switch() probably shaved 8KB out of 48KB.

    So I'm disposed to use them, even to this day.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    Quote Originally Posted by Old Pedant View Post
    Way back in the dawn of civilization, the way I learned C was to be assigned to write a compiler for the language. (Never mind that I'd never written a compiler and never seen the language before.) This was for a Z-80 computer (8-bit processor, 64K-byte address space) and we had about 48KB of memory to work with. And, yes, we wrote the compiler itself in C and, at the end, used it to compile itself.

    I'm not sure I could have fit the code into 48KB without using ternary operators. You'd be amazed at how efficient the machine code is that they can produce compared to conventional if/else. That and switch() probably shaved 8KB out of 48KB.

    So I'm disposed to use them, even to this day.
    I also programmed in the days of 8086 processors when every precious byte counted. But those days have (thankfully) passed away and I don't consider that saving a few bytes nowadays is worth any loss of clarity. But it is a case of chacun à son goût.

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,648 Times in 4,610 Posts
    LOL! I just meant "old habits die hard...especially for old people." Kind of making fun of myself.

    8086? Wow, you did start late, didn't you. <grin style="monstrous"/>

    (I actually started with 4-bit processors, in 1972.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #14
    New Coder
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks so much for all the info guys. You helped save my but on this assignment.

    And yes, I am kicking myself about the Semi-colon.


  •  

    Posting Permissions

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