...

View Full Version : Compiler issue with else and if



bookittysdad
12-07-2010, 06:02 PM
I am a student. I have eliminated all errors except this one. :mad:


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. :confused: I am in a time crunch with this chapter ending tomorrow night. :eek:

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.


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

import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;

public class Reservations extends Frame implements ActionListener
{
color lightRed = new color(255, 90, 90);
color lightGreen = new color(140, 215, 40);

Rooms room = new Rooms(5, 3);

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=1; i<9; i++)
{
roomDisplay[i] = new TextArea(null,3,5,3);
if(i<6)
roomDisplay[i].setText("Room " + i + "NonSmoking");
else
roomDisplay[i].setText("Room " + i + "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 = 8; i<=20; i++)
numberOfGuests.add(String.valueOf(i));
inputPanel.add(nonSmoking);
inputPanel.add(Smoking);

//adds panel to Frame
add(buttonPanel, BorderLayout.South);
add(inputPanel, BorderLayout.Center);
add(roomPanel, BorderLayout.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

Philip M
12-07-2010, 06:21 PM
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

bookittysdad
12-07-2010, 06:36 PM
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.:thumbsup:

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

Philip M
12-07-2010, 06:43 PM
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/forumdisplay.php?s=&forumid=2

Old Pedant
12-07-2010, 07:52 PM
Ready to kick yourself?



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#.

Old Pedant
12-07-2010, 07:58 PM
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:


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

if(i<6)
roomDisplay[i].setText("Room " + i + "NonSmoking");
else
roomDisplay[i].setText("Room " + i + "Smoking");
*** YES ***


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 {...}.)

Old Pedant
12-07-2010, 07:58 PM
See, Philip, I'm coming around. <grin/> Though not completely.

Philip M
12-07-2010, 08:01 PM
See, Philip, I'm coming around. <grin/> Though not completely.

Glad to know that you are learning, albeit slowly. :D:D

Old Pedant
12-07-2010, 08:07 PM
Of course, even better than

if(i<6) {
roomDisplay[i].setText("Room " + i + "NonSmoking");
} else {
roomDisplay[i].setText("Room " + i + "Smoking");
}
is

roomDisplay[i].setText("Room " + i + ( i < 6 ? "NonSmoking" : "Smoking") );

Philip M
12-07-2010, 08:13 PM
Of course, even better than

if(i<6) {
roomDisplay[i].setText("Room " + i + "NonSmoking");
} else {
roomDisplay[i].setText("Room " + i + "Smoking");
}
is

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.

Old Pedant
12-07-2010, 08:24 PM
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.

Philip M
12-07-2010, 08:38 PM
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.

Old Pedant
12-07-2010, 09:00 PM
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.)

bookittysdad
12-08-2010, 12:14 AM
Thanks so much for all the info guys. You helped save my but on this assignment. :thumbsup:

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum