trying to get this program to run and getting an odd error...does anyone know what I have to do

//packages to import

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class PetAdviceApplet extends Applet implements ItemListener
{
	//declare variables and construct components
	int housing;
	double hours;
	String pet;


	Label promptLabel = new Label ("Enter the average number of hours you are home per week ");
	TextField hoursField = new TextField(15);

	CheckboxGroup housingGroup = new CheckboxGroup();
	Checkbox houseBox = new Checkbox("House", false, housingGroup);
	Checkbox apartmentBox = new Checkbox("Apartment", false, housingGroup);
	Checkbox dormitoryBox = new Checkbox("Dormitory", false, housingGroup);
	Checkbox hiddenBox = new Checkbox("",true,housingGroup);

	Label housingLabel = new Label ("Select your type of housing to determine the best pet ");

	Label outputLabel = new Label("Enter a valid number of hours between 0 and 168 ");

	public void init()
	{
		setBackground(Color.red);
		setForeground(Color.black);
		add(promptLabel);
		add(hoursField);
		hoursField.requestFocus();
		hoursField.setForeground(Color.black);
		add(houseBox);
		houseBox.addItemListener(this);
		add(apartmentBox);
		apartmentBox.addItemListener(this);
		add(dormitoryBox);
		dormitoryBox.addItemListener(this);
		add(housingLabel);
		add(outputLabel);
	} //end init()

	//This method is triggered by the user clicking an option button
	public void itemStateChanged(ItemEvent choice)
	{
		try
		{
			housing = getHousing();
			hours = getHours()	;
			pet = getPet(hours, housing);
			output(pet);
		} //end try

	catch(NumberFormatException e)
		{
			outputLabel.setText("Enter a valid number for hours between 0 and 168 ");
			hiddenBox.setState(true);
			hoursField.setText("");
			hoursField.requestFocus();
		} //end catch
	}

	public double getHours()
	{
		double hours = Double.parseDouble(hoursField.getText());
		if ((hours <= 0) || (hours >= 168)) throw new NumberFormatException();

		return hours;
	} //end getHours()

	public int getHousing()
	{
		int housing = 0;
		if (houseBox.getState()) housing = 1;
		else
		if (apartmentBox.getState()) housing = 2;
		else
		if (dormitoryBox.getState()) housing = 3;

		return housing;
	} //end getHousing()

	public String getPet(double hours, int housing)
				{
					//declare method variables

					String pet = "";


					switch(housing)
					{
						case 1: //pot-bellied pig
						if ((housing == 1) && (hours>=18))
						{
						pet = "Pot-bellied pig";
						}
						case 2: //dog
						if((housing == 1) && (hours>=10) && (hours < 18))
						{
						pet = "Dog";
						}

						case 3: //snake
						if ((housing == 1) && (hours < 10))
						{
						pet = "Snake";
						}

						case 4: //cat
						if ((housing == 2) && (hours >= 10))
						{
						pet = "Cat";
						}

						case 5: //hamster
						if ((housing == 2) && (hours < 10))
						{
						pet = "Hamster";
						}

						case 6: //fish
						if ((housing == 3) && (hours >= 6))
						{
						pet = "Fish";
						}

						case 7: //ant farm
						if ((housing == 3) && (hours < 6))
						{
						pet = "Ant farm";
						}

						break;

					}//end switch

					return pet;

		}//end getComm
	public void output(String pet)
	{
		outputLabel.setText ("According to your requirements, I would recommend a ",(pet));
	} //end output
}//end class

this is the error.....

H:\JAVA\PetAdviceApplet.java:157: setText(java.lang.String) in java.awt.Label cannot be applied to (java.lang.String,java.lang.String)
outputLabel.setText ("According to your requirements, I would recommend a ",(pet));

You need to concatenate the string with "+". outputLabel.setText ("According to your requirements, I would recommend a "+pet); . Your current statement is being read as a call with 2 parameters.

having an additional issue with my program. It will not compile and when I changed things around, it showed a compile but the clear button did not do what it was supposed to do and the format is off this is my code:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class PetAdviceApplet extends Applet implements ItemListener
{
	//declare variables and construct components
	int housing;
	double hours;
	String pet;


	Label promptLabel = new Label ("Enter the average number of hours you are home per week ");
	TextField hoursField = new TextField(15);

	CheckboxGroup housingGroup = new CheckboxGroup();
	Checkbox houseBox = new Checkbox("House", false, housingGroup);
	Checkbox apartmentBox = new Checkbox("Apartment", false, housingGroup);
	Checkbox dormitoryBox = new Checkbox("Dormitory", false, housingGroup);
	Checkbox hiddenBox = new Checkbox("",true,housingGroup);

	Label housingLabel = new Label ("Select your type of housing to determine the best pet ");

	Label outputLabel = new Label (" ");

	Button clearButton = new Button("Clear Fields");

	public void init()
	{
		setBackground(Color.red);
		setForeground(Color.black);
		add(promptLabel);
		add(hoursField);
		hoursField.requestFocus();
		hoursField.setForeground(Color.black);
		add(houseBox);
		houseBox.addItemListener(this);
		add(apartmentBox);
		apartmentBox.addItemListener(this);
		add(dormitoryBox);
		dormitoryBox.addItemListener(this);
		add(housingLabel);
		add(outputLabel);

		add(clearButton);
	} //end init()

	//This method is triggered by the user clicking an option button
	public void itemStateChanged(ItemEvent choice)
	{
		try
		{
			housing = getHousing();
			hours = getHours()	;
			pet = getPet(hours, housing);
			output(pet);
		} //end try

	catch(NumberFormatException e)
		{
			outputLabel.setText("Enter a valid number for hours between 0 and 168 ");
			hiddenBox.setState(true);
			hoursField.setText("");
			hoursField.requestFocus();
		} //end catch
	}

	public double getHours()
	{
		double hours = Double.parseDouble(hoursField.getText());
		if ((hours <= 0) || (hours >= 168)) throw new NumberFormatException();

		return hours;
	} //end getHours()

	public int getHousing()
	{
		int housing = 0;
		if (houseBox.getState()) housing = 1;
		else
		if (apartmentBox.getState()) housing = 2;
		else
		if (dormitoryBox.getState()) housing = 3;

		return housing;
	} //end getHousing()

	public String getPet(double hours, int housing)
				{
					//declare method variables

					String pet = "";


					switch(housing)
					{
						case 1: //pot-bellied pig
						if ((housing == 1) && (hours>=18))
						{
						pet = "Pot-bellied pig";
						}
						case 2: //dog
						if((housing == 1) && (hours>=10) && (hours < 18))
						{
						pet = "Dog";
						}

						case 3: //snake
						if ((housing == 1) && (hours < 10))
						{
						pet = "Snake";
						}

						case 4: //cat
						if ((housing == 2) && (hours >= 10))
						{
						pet = "Cat";
						}

						case 5: //hamster
						if ((housing == 2) && (hours < 10))
						{
						pet = "Hamster";
						}

						case 6: //fish
						if ((housing == 3) && (hours >= 6))
						{
						pet = "Fish";
						}

						case 7: //ant farm
						if ((housing == 3) && (hours < 6))
						{
						pet = "Ant farm";
						}

						break;

					}//end switch

					return pet;

		}//end getComm
	public void output(String pet)
	{
		outputLabel.setText ("According to your requirements, I would recommend a " + pet);
	} //end output
}//end class

output should be
Initial screen:

Enter the average number of hours you are home per week
House Apartment dormitory

Select your type of housing to determine the best pet

Clear Fields Button

Screen with valid Input


Enter the average number of hours you are home per week
House Apartment dormitory

Select your type of housing to determine the best pet
According to your requirements I would recommend a ....

Clear Fields Button

Invalid screen
Enter the average number of hours you are home per week
House Apartment dormitory

Select your type of housing to determine the best pet
Enter a valid number of hours between 0 and 168
Clear Fields Button

I don't know what "my format is off" is supposed to mean, so you'll have to clarify that more.

The clear button doesn't work because you haven't written any code for it to do anything. You need to implement and attach an ActionListener for that.

I apologize for not being clear. For one we have never used more than one listener at a time so I am not sure on how to do the calculate. Seconly when i do run the applet the out put shows when I expand the applet window but not when I don't if that makes sense. Also the clear button is supposed to be under everything and how I had it earlier it was to the side of the last line. I hope this helps on some of my problems if anyone could help id appreciate it because I have a midnight dead line and just need some help finishing the program

As far as the listener, read the link that I posted above on how to write an action listener.

The reason the output doesn't show is that the container hasn't updated the component spacing to accommodate the longer text that you put into the output field. Adding a call to validate() after you set the text would fix that

public void output(String pet) {
        outputLabel.setText("According to your requirements, I would recommend a "+pet);
        validate();
    }

You would have much more control of the layout if you used something besides the default FlowLayout. See this tutorial on other layout options: http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html

I know it is supposed to be #
public class PetAdviceApplet extends Applet implements ActionListener


That is no problem...my only problem is that there is already an ItemListener and have never done two listeners but I know for sure I need an Item Listener but confused on how to put a second one in there

You can implement as many interfaces as you like

public class PetAdviceApplet extends Applet implements ActionListener,ItemListener

. Then you need to add the actionPerformed() method to your class to handle the event.

o ok I got you now sorry for sounding dumb we have never done two before so I got it now....was alot easier than I made it

One last question and I am done...It is showing to the right of the output how can I put the calculate button under the output

public void actionPerformed(ActionEvent e)
{






	} //end actionPerformed

	//This method is triggered by the user clicking an option button
	public void itemStateChanged(ItemEvent choice)
	{
		try
		{
			housing = getHousing();
			hours = getHours()	;
			pet = getPet(hours, housing);
			output(pet);
		} //end try

	catch(NumberFormatException e)
		{
			outputLabel.setText("Enter a valid number for hours between 0 and 168 ");
			hiddenBox.setState(true);
			hoursField.setText("");
			hoursField.requestFocus();
		} //end catch
	}

	public double getHours()
	{
		double hours = Double.parseDouble(hoursField.getText());
		if ((hours <= 0) || (hours >= 168)) throw new NumberFormatException();

		return hours;
	} //end getHours()

	public int getHousing()
	{
		int housing = 0;
		if (houseBox.getState()) housing = 1;
		else
		if (apartmentBox.getState()) housing = 2;
		else
		if (dormitoryBox.getState()) housing = 3;

		return housing;
	} //end getHousing()

	public String getPet(double hours, int housing)
				{
					//declare method variables

					String pet = "";


					switch(housing)
					{
						case 1: //pot-bellied pig
						if ((housing == 1) && (hours>=18))
						{
						pet = "Pot-bellied pig";
						}
						case 2: //dog
						if((housing == 1) && (hours>=10) && (hours < 18))
						{
						pet = "Dog";
						}

						case 3: //snake
						if ((housing == 1) && (hours < 10))
						{
						pet = "Snake";
						}

						case 4: //cat
						if ((housing == 2) && (hours >= 10))
						{
						pet = "Cat";
						}

						case 5: //hamster
						if ((housing == 2) && (hours < 10))
						{
						pet = "Hamster";
						}

						case 6: //fish
						if ((housing == 3) && (hours >= 6))
						{
						pet = "Fish";
						}

						case 7: //ant farm
						if ((housing == 3) && (hours < 6))
						{
						pet = "Ant farm";
						}

						break;

					}//end switch

					return pet;

		}//end getComm
	public void output(String pet)
	{
		outputLabel.setText ("According to your requirements, I would recommend a " + pet);
		validate();
	} //end output
}//end class

can anyone help me on how to get this clear button to work

I have everything done but the checkboxes to be clear

public void actionPerformed(ActionEvent e)
{

	//clear the applet
	hoursField.setText("");
	outputLabel.setText("");
	





	} //end actionPerformed
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.