Problems with Applet

Hello,

I'm having problems with my Applet. Basically the issue is that once I'm in the roomInfoScreen and the if condition does not apply anymore I get an exception and I don't know how to fix this problem .

I will appreciate any help.

Thank you very much

CODE

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class InputApplet extends JApplet implements ActionListener

{

int NUM_ROOMS; //rooms in the house

int clickcount=0;

Container con = getContentPane();

JPanel roomNumScreen = new JPanel(new FlowLayout());

JPanel roomInfoScreen = new JPanel(new FlowLayout());

JPanel sortingScreen = new JPanel(new FlowLayout());

JPanel outputScreen = new JPanel(new FlowLayout());

JPanel errorScreen = new JPanel (new FlowLayout());

JLabel labelRoomNum = new JLabel("Please enter the number of rooms in the your house");

JTextField textRoomNum = new JTextField("", 3);

JButton buttonNumSubmit = new JButton("Submit");

JLabel errorLabel = new JLabel("Input must be a number, please try again");

JButton errorButton = new JButton("Retry");

JLabel roomNumber = new JLabel("Room#1 information: ");

JLabel roomName = new JLabel("Room Name: ");

JLabel roomWidth = new JLabel("Room Width: ");

JLabel roomLength = new JLabel("Room Length: ");

JLabel roomFloor = new JLabel("Floor Room can be found on: ");

JTextField textRoomName = new JTextField("", 40);

JTextField textRoomWidth = new JTextField("", 40);

JTextField textRoomLength = new JTextField("", 40);

JTextField textRoomFloor = new JTextField("", 40);

JButton buttonRoomInfo = new JButton("Submit");

JLabel sortingLabel = new JLabel("Please make your reporting selection: ");

JButton sortArea = new JButton("Sort by Area");

JButton sortRoomName = new JButton("Sort by Room Name");

JButton sortFloorNum = new JButton("Sort by Floor Number");

RoomClass[] room; // instanciating the RoomClass

public void init()

{

roomNumScreen.add(labelRoomNum);

roomNumScreen.add(textRoomNum);

roomNumScreen.add(buttonNumSubmit);

textRoomNum.requestFocus();

buttonNumSubmit.addActionListener(this);

con.add(roomNumScreen);

errorScreen.add(errorLabel);

errorScreen.add(errorButton);

errorButton.addActionListener(this);

roomInfoScreen.add(roomNumber);

roomInfoScreen.add(roomName);

roomInfoScreen.add(textRoomName);

roomInfoScreen.add(roomWidth);

roomInfoScreen.add(textRoomWidth);

roomInfoScreen.add(roomLength);

roomInfoScreen.add(textRoomLength);

roomInfoScreen.add(roomFloor);

roomInfoScreen.add(textRoomFloor);

roomInfoScreen.add(buttonRoomInfo);

buttonRoomInfo.addActionListener(this);

sortingScreen.add(sortingLabel);

sortingScreen.add(sortArea);

sortingScreen.add(sortRoomName);

sortingScreen.add(sortFloorNum);

sortArea.addActionListener(this);

sortRoomName.addActionListener(this);

sortFloorNum.addActionListener(this);

}

public void actionPerformed(ActionEvent event)

{

String stgRoomNum, roomName, stgRoomWidth, stgRoomLength, stgRoomFloor; //name of the room

intfloorNum, numRetries =3, x; //variables for floor number and number of retries.

double roomLength, roomWidth; //variables for lengh,width,

Object source = event.getSource();

if(source == buttonNumSubmit)

{

stgRoomNum = textRoomNum.getText();

System.out.println("Inside the roomNumScreen Panel - where textRoomNum input has meaning, and stgRoomNum="+stgRoomNum);

if(stgRoomNum.length() == 0)

{

con.remove(roomNumScreen);

con.add(errorScreen);

super.validate();

repaint();

if(source == errorButton)

{

con.remove(errorScreen);

con.add(roomNumScreen);

textRoomNum.setText("");

}

}

else

{

NUM_ROOMS = Integer.parseInt(stgRoomNum);

room = new RoomClass[NUM_ROOMS];

con.remove(roomNumScreen);

con.add(roomInfoScreen);

super.validate();

repaint();

}

}

if(source == buttonRoomInfo)

{

if(clickcount <= NUM_ROOMS)

{

clickcount++;

System.out.println("Inside the roomInfoScreen Panel " + clickcount + " :");

String roomNum = "Room #" + (clickcount+1) + " information: ";

roomNumber.setText(roomNum);

roomName = textRoomName.getText();

stgRoomWidth = textRoomWidth.getText();

roomWidth = Double.parseDouble(stgRoomWidth);

stgRoomLength = textRoomLength.getText();

roomLength = Double.parseDouble(stgRoomLength);

stgRoomFloor = textRoomFloor.getText();

floorNum = Integer.parseInt(stgRoomFloor);

// this passes the values to the constructor in the RoomClass class

room[clickcount] = new RoomClass(roomName, roomLength, roomWidth, floorNum);

textRoomName.setText("");

textRoomWidth.setText("");

textRoomLength.setText("");

textRoomFloor.setText("");

}

else

{

System.out.println("Inside the else statement");

con.remove(roomInfoScreen);

con.add(sortingScreen);

super.validate();

repaint();

}

}

if (source == sortRoomName)

{

SortClass.sorting(room, "Name");

}

else if (source == sortFloorNum)

{

SortClass.sorting(room, "Level");

}

else if (source == sortArea)

{

SortClass.sorting(room, "Square Feet");

}

}

}

[5693 byte] By [CaMcCa] at [2007-11-27 9:42:30]
# 1

1) Welcome to the forum.

2) Please use code tags whenever posting code. It makes code easier to read and therefore makes it more likely that someone will actually read your code and help you. You can find out about them here:

http://forum.java.sun.com/help.jspa?sec=formatting

3) Without code for the RoomClass your code is uncompilable and thus uncorrectable. Also, rather than say "I get an exception" you need to give details. If you get any help here, it is from volunteers who do this out of altruistic desire. Your job is to make their job as easy as possible. Best of luck!

/Pete

petes1234a at 2007-7-12 23:46:06 > top of Java-index,Java Essentials,New To Java...
# 2
Thank you for the suggestion. I would still appreciate any help anyone can give me.Thank you
CaMcCa at 2007-7-12 23:46:06 > top of Java-index,Java Essentials,New To Java...
# 3
> Thank you for the suggestion. > I would still appreciate any help anyone can give> me.Please re-read my post.
petes1234a at 2007-7-12 23:46:06 > top of Java-index,Java Essentials,New To Java...
# 4

Here is the RoomClass code.

Thank you.

Class code:

//RoomClass.java

/** The RoomClass contains all the attributes for this constructor */

public class RoomClass

{

String roomName;

doubleroomLength, roomWidth, squareSize;

intfloorNum;

public RoomClass(String name, double length, double width, int floor) // sets room attributes

{

roomName = name;

roomLength = length;

roomWidth = width;

floorNum = floor;

}

public String getRoomName() // get room name

{

return roomName;

}

public double getRoomLength()// get room length

{

return roomLength;

}

public double getRoomWidth()// get room width

{

return roomWidth;

}

public int getFloorNum() // get room location

{

return floorNum;

}

public double getSquareSize() // calculates room square feet

{

squareSize = roomLength * roomWidth;

return squareSize;

}

}

Message was edited by:

CaMcC

CaMcCa at 2007-7-12 23:46:06 > top of Java-index,Java Essentials,New To Java...
# 5
post the code of the SortClass plz
java_2006a at 2007-7-12 23:46:06 > top of Java-index,Java Essentials,New To Java...
# 6

use this :

if (clickcount < NUM_ROOMS-1)

instead of this :

if (clickcount <= NUM_ROOMS)

You have to handle the case of 'single room' (NUM_ROOMS=1) like the following:

if (source == buttonRoomInfo) {

if(NUM_ROOMS==1){

................

}else if(NUM_ROOMS>1){

else if (clickcount < NUM_ROOMS-1) {

..........

}

}

}

Hope That Helps

java_2006a at 2007-7-12 23:46:06 > top of Java-index,Java Essentials,New To Java...
# 7

Here is sort class. Thank you.

public class SortClass

{

public static void sorting(RoomClass[] room, String sortRoom)

{

boolean switcher;

RoomClass holder;// temporary holder for array while being sorted

for (int a = 0; a < room.length - 1; ++a)

for (int b = 0; b < room.length - 1; ++b)

{

if (sortRoom.equals("Name")) //sorts by room name

{

if(room.getRoomName().compareTo(room[b + 1].getRoomName()) > 0)

{

holder = room;

room = room[b + 1];

room[b + 1] = holder;

}

}

else if (sortRoom.equals("Level")) // sorts by floor number

{

if(room.getFloorNum() > room[b + 1].getFloorNum())

{

holder = room;

room = room[b + 1];

room[b + 1] = holder;

}

}

else if (sortRoom.equals("Square Feet")) // sorts by square footage

{

if(room.getSquareSize() < room[b + 1].getSquareSize())

{

holder = room;

room = room[b + 1];

room[b + 1] = holder;

}

}

}

}

}

CaMcCa at 2007-7-12 23:46:06 > top of Java-index,Java Essentials,New To Java...