I have never posted anything in here for java so if I don'tuse the code tags right I apologize...anyways I compiled an assignment in text pad and have a ton or errors and I was wanting to know if it is something minor or I am really that stupid...thanks

Here is what the assignment says:

Write an application (PetAdvice.java) that recommends a pet for a user based on the user’s lifestyle. Prompt the user to enter whether he or she lives in an apartment, house, or dormitory (1, 2, or 3… 4 to quit) and the average number or hours the user is home per week.

Additional hints/requirements:
1. Do not use the default values for the title bar caption – see example below.
2. For your icons, use the question for input, the information “I” for output, and the error for error message dialog boxes.
3. You are required to use IF statements and case statements at some point in the program. I combined some IF statements with my case statements to come up with the pet in the getPet() method.
4. You should allow decimals for the hours input.
5. Make sure you provide reasonableness checks when necessary.
6. If the user makes an invalid entry, allow them to re-enter their data (loop back). However, you don’t need to loop back to the beginning at the end of your program. Once your user clicks the last OK, you can terminate.
7. You will be required to use multiple methods for this application (like we were doing with our Chapter 4 in-class exercises). You don’t have to use my exact methods, but here is what I used: getHousing(), getHours(), getPet(), output(), and finish().
8. Termination: program your cancel buttons to terminate in addition to terminating when your user selects 4 on the first screen.


Print your recommendation based on the following table:
Residence Hours Home Recommendations
House 18 or more Pot-bellied pig
House 10 to <18 Dog
House Fewer than 10 Snake
Apartment 10 or more Cat
Apartment Fewer than 10 Hamster
Dormitory 6 or more Fish
Dormitory Fewer than 6 Ant farm


this is what I have so far and if the code tags dont come out right please let me know

/*

Programmer:	Jim Johnson
Date:		3/18/08
Filename:	PetAdvice.java
Purpose:	

*/

//packages to import

import javax.swing.JOptionPane;


public class PetAdvice
{
	public static void main(String[] args)
	{
		//declare class variables
		int housing;
		double hours;
		String pet;

		//call method
		housing = getHousing();
		hours = getHours();
		pet = getPet();
		output();
		finish();
	}//end main()

		//the output() method displays the pet recommended
			public static void output(String pet)
			{
;

				//display output
				JOptionPane.showMessageDialog(null, "According to your requirements, I would recommend a ", + pet, "Pet Advice: Recommendation", JOptionPane.INFORMATION_MESSAGE);

		}// end output



			//the getPet() method accepts the housing and hours and returns a pet
			public static String getPet(String pet)
			{
				//declare method variables
				String pet;

				switch(housing)
				{
					case 1: //pot-bellied pig
					if ((housing = 1) && (hours>=18))
					pet = "pot-bellied pig";
					else
					break;
					case 2: //dog
					if ((housing = 1) && (hours>=10) && (hours < 18))
					pet = "dog";
					break;
					case 3: //snake
					if ((housing = 1) && (hours < 10))
					pet = "snake";
					break;
					case 4: //cat
					if ((housing = 2) && (hours >= 10))
					pet = "cat";
					break;
					case 5: //hamster
					if ((housing = 2) && (hours < 10))
					pet = "hamster";
					break;
					case 6: //fish
					if ((housing = 3) && (hours >= 6))
					pet = fish;
					break;
					case 7: //ant farm
					if ((housing = 3) && (hours < 6))
					pet = "ant farm";
					break;
				}//end switch


				//.return value to the calling method
				return commission;

		}//end getComm




//the getHours() method asks the user to input hours and validate it

	public static double getHours()
	{
		//declare method variables
		double hours = 0.0;
		boolean done = false;

		//loop while not done
		while(!done)
		{
			String answer = JOptionPane.showInputDialog(null, "Enter the average number of hours\nyou are home per week:");

			//See if the cancel buttom was clicked....this will only work if the input is brought in as a a string
			if(answer == null) finish();

			//validate the input
			try
			{
				//is answer a valid double
				hours = Double.parseDouble(answer);

				//is sales reasonable?
				if(hours <= 0) throw new NumberFormatException();
				//if the value is valid and reasonable, get out of the while loop
				else done = true;
			} //end try
			catch(NumberFormatException e)
			{
				JOptionPane.showMessageDialog(null, "Your entry was not in the proper format.\nPlease enter a positive number for hours", "Error", JOptionPane. ERROR_MESSAGE);

			} //end catch

	}	//end while


		return hours;
	} //end getHours()

		//the getHousing() method retries a housing option from the user and validates it.
		public static int getHousing()
		{
			//declare method variables
			int housing = 0;
			boolean done = false;

			//loop while not done
			while(!done)
			{
				try
				{
					String message = "Enter your type of residence:" + "\n\n1) House\n2) Apartment\n3) Dormitory\n4) Quit the Application\n";

					code = Integer.parseInt(JOptionPane.showInputDialog(null,message));

					//test for valid codes 1, 2, 3, or 4
					if(code<1 || code>4) throw new NumberFormatException();
					else done = true;
				}
				catch(NumberFormatException e)
				{
					JOptionPane.showMessageDialog(null,"Please enter a 1, 2, 3, or 4.", "Error", JOptionPane.INFORMATION_MESSAGE);
				}
			}
			return code;
	}


	//the finish() method successfully terminates the application
	public static void finish()
	{
		System.exit(0);
	} //end finish()


} //end class

Recommended Answers

All 9 Replies

What errors are you getting and what don't you understand about them?
It's vitally important that you learn to understand and interpret error messages.
As long as you can't, you'll never be able to successfully complete a piece of software in any language or using any tool.

error messages....and by the way this is my first post in a java forum....so it is not like I am trying to get answers....as you can see I have alot of stuff in here and I am just confused because it seems like whenever I have this many errors one thing fixes most of them so if anyone can actually help me with this please let me know

F:\JAVA\PetAdvice.java:27: getPet(java.lang.String) in PetAdvice cannot be applied to ()
pet = getPet();
^
F:\JAVA\PetAdvice.java:28: output(java.lang.String) in PetAdvice cannot be applied to ()
output();
^
F:\JAVA\PetAdvice.java:38: operator + cannot be applied to java.lang.String
JOptionPane.showMessageDialog(null, "According to your requirements, I would recommend a ", + pet, "Pet Advice: Recommendation", JOptionPane.INFORMATION_MESSAGE);
^
F:\JAVA\PetAdvice.java:48: pet is already defined in getPet(java.lang.String)
String pet;
^
F:\JAVA\PetAdvice.java:50: cannot find symbol
symbol : variable housing
location: class PetAdvice
switch(housing)
^
F:\JAVA\PetAdvice.java:53: cannot find symbol
symbol : variable housing
location: class PetAdvice
if ((housing = 1) && (hours>=18))
^
F:\JAVA\PetAdvice.java:53: cannot find symbol
symbol : variable hours
location: class PetAdvice
if ((housing = 1) && (hours>=18))
^
F:\JAVA\PetAdvice.java:58: cannot find symbol
symbol : variable housing
location: class PetAdvice
if ((housing = 1) && (hours>=10) && (hours < 18))
^
F:\JAVA\PetAdvice.java:58: cannot find symbol
symbol : variable hours
location: class PetAdvice
if ((housing = 1) && (hours>=10) && (hours < 18))
^
F:\JAVA\PetAdvice.java:58: cannot find symbol
symbol : variable hours
location: class PetAdvice
if ((housing = 1) && (hours>=10) && (hours < 18))
^
F:\JAVA\PetAdvice.java:62: cannot find symbol
symbol : variable housing
location: class PetAdvice
if ((housing = 1) && (hours < 10))
^
F:\JAVA\PetAdvice.java:62: cannot find symbol
symbol : variable hours
location: class PetAdvice
if ((housing = 1) && (hours < 10))
^
F:\JAVA\PetAdvice.java:66: cannot find symbol
symbol : variable housing
location: class PetAdvice
if ((housing = 2) && (hours >= 10))
^
F:\JAVA\PetAdvice.java:66: cannot find symbol
symbol : variable hours
location: class PetAdvice
if ((housing = 2) && (hours >= 10))
^
F:\JAVA\PetAdvice.java:70: cannot find symbol
symbol : variable housing
location: class PetAdvice
if ((housing = 2) && (hours < 10))
^
F:\JAVA\PetAdvice.java:70: cannot find symbol
symbol : variable hours
location: class PetAdvice
if ((housing = 2) && (hours < 10))
^
F:\JAVA\PetAdvice.java:74: cannot find symbol
symbol : variable housing
location: class PetAdvice
if ((housing = 3) && (hours >= 6))
^
F:\JAVA\PetAdvice.java:74: cannot find symbol
symbol : variable hours
location: class PetAdvice
if ((housing = 3) && (hours >= 6))
^
F:\JAVA\PetAdvice.java:75: cannot find symbol
symbol : variable fish
location: class PetAdvice
pet = fish;
^
F:\JAVA\PetAdvice.java:78: cannot find symbol
symbol : variable housing
location: class PetAdvice
if ((housing = 3) && (hours < 6))
^
F:\JAVA\PetAdvice.java:78: cannot find symbol
symbol : variable hours
location: class PetAdvice
if ((housing = 3) && (hours < 6))
^
F:\JAVA\PetAdvice.java:85: cannot find symbol
symbol : variable commission
location: class PetAdvice
return commission;
^
F:\JAVA\PetAdvice.java:145: cannot find symbol
symbol : variable code
location: class PetAdvice
code = Integer.parseInt(JOptionPane.showInputDialog(null,message));
^
F:\JAVA\PetAdvice.java:148: cannot find symbol
symbol : variable code
location: class PetAdvice
if(code<1 || code>4) throw new NumberFormatException();
^
F:\JAVA\PetAdvice.java:148: cannot find symbol
symbol : variable code
location: class PetAdvice
if(code<1 || code>4) throw new NumberFormatException();
^
F:\JAVA\PetAdvice.java:156: cannot find symbol
symbol : variable code
location: class PetAdvice
return code;
^
26 errors

Tool completed with exit code 1

actually stratch that...i pretty much have two errors problems...one is with the if statements and the other is in the getPet.....

F:\JAVA\PetAdvice.java:27: getPet(java.lang.String) in PetAdvice cannot be applied to ()
pet = getPet();
^
F:\JAVA\PetAdvice.java:28: output(java.lang.String) in PetAdvice cannot be applied to ()
output();
^
F:\JAVA\PetAdvice.java:55: operator && cannot be applied to int,boolean
if ((housing = 1) && (hours>=18))
^
F:\JAVA\PetAdvice.java:60: operator && cannot be applied to int,boolean
if ((housing = 1) && (hours>=10) && (hours < 18))
^
F:\JAVA\PetAdvice.java:64: operator && cannot be applied to int,boolean
if ((housing = 1) && (hours < 10))
^
F:\JAVA\PetAdvice.java:68: operator && cannot be applied to int,boolean
if ((housing = 2) && (hours >= 10))
^
F:\JAVA\PetAdvice.java:72: operator && cannot be applied to int,boolean
if ((housing = 2) && (hours < 10))
^
F:\JAVA\PetAdvice.java:76: operator && cannot be applied to int,boolean
if ((housing = 3) && (hours >= 6))
^
F:\JAVA\PetAdvice.java:80: operator && cannot be applied to int,boolean
if ((housing = 3) && (hours < 6))

ok updating again my only error is in the switch with the &&

I see some potential problems in the "if" statements where you were using = instead off == in places. You say there's only one error now and that you've made revisions, so it may be good to post your revised code along with the error and the line number.

I think Vernon's suggestion will fix your problems. This is a common mistake when first learning Java and other C-style programming languages. Saying '=' is an assignment to a value statement, saying '==' is a test for equality. In other words the statement

housing = 2

means "set 'housing' equal to 2" while the statement

housing == 2

means "is housing currently equal to the number 2?" which can only be a true or false (boolean) answer.

/*

Programmer:	Jim Johnson
Date:		3/18/08
Filename:	PetAdvice.java
Purpose:	This program determines the best choice for a pet using five methods: getHousing(), getHours(), getPet(), output(), and finish()

*/

//packages to import

import javax.swing.JOptionPane;


public class PetAdvice
{
	public static void main(String[] args)
	{
		//declare class variables
		int housing;
		double hours;
		String pet;

		//call method
		housing = getHousing();
		hours = getHours();
		pet = getPet(hours, housing);
		output(pet);
		finish();
	}//end main()

		//the output() method displays the pet recommended
			public static void output(String pet)
			{
;

				//display output
				JOptionPane.showMessageDialog(null, "According to your requirements, I would recommend a " + pet, "Pet Advice: Recommendation", JOptionPane.INFORMATION_MESSAGE);

		}// end output



			//the getPet() method accepts the housing and hours and returns a pet
			public static 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";
					break;
					case 2: //dog
					if ((housing == 1) && (hours>=10) && (hours < 18))
					pet = "dog";
					break;
					case 3: //snake
					if ((housing == 1) && (hours < 10))
					pet = "snake";
					break;
					case 4: //cat
					if ((housing == 2) && (hours >= 10))
					pet = "cat";
					break;
					case 5: //hamster
					if ((housing == 2) && (hours < 10))
					pet = "hamster";
					break;
					case 6: //fish
					if ((housing == 3) && (hours >= 6))
					pet = "fish";
					break;
					case 7: //ant farm
					if ((housing == 3) && (hours < 6))
					pet = "ant farm";
					break;

				}//end switch


				//.return value to the calling method

				return pet;

		}//end getComm




//the getHours() method asks the user to input hours and validate it

	public static double getHours()
	{
		//declare method variables
		double hours = 0.0;
		boolean done = false;

		//loop while not done
		while(!done)
		{
			String answer = JOptionPane.showInputDialog(null, "Enter the average number of hours\nyou are home per week:", "Pet Advice: Hours", JOptionPane.QUESTION_MESSAGE);

			//See if the cancel buttom was clicked....this will only work if the input is brought in as a a string
			if(answer == null) finish();

			//validate the input
			try
			{
				//is answer a valid double
				hours = Double.parseDouble(answer);

				//is sales reasonable?
				if(hours <= 0) throw new NumberFormatException();
				//if the value is valid and reasonable, get out of the while loop
				else done = true;
			} //end try
			catch(NumberFormatException e)
			{
				JOptionPane.showMessageDialog(null, "Your entry was not in the proper format.\nPlease enter a positive number for hours", "Error", JOptionPane. ERROR_MESSAGE);

			} //end catch

	}	//end while


		return hours;
	} //end getHours()

		//the getHousing() method retries a housing option from the user and validates it.
		public static int getHousing()
		{
			//declare method variables
			int housing = 0;
			boolean done = false;

			//loop while not done
			while(!done)
			{
				try
				{
					String message = JOptionPane.showInputDialog(null, "Enter your type of residence:" + "\n\n1) House\n2) Apartment\n3) Dormitory\n4) Quit the Application\n", "Pet Advice: Housing", JOptionPane.QUESTION_MESSAGE);

					housing = Integer.parseInt(JOptionPane.showInputDialog(null,message));

					//test for valid housing 1, 2, 3, or 4
					if(housing<1 || housing>4) throw new NumberFormatException();
					else done = true;
				}
				catch(NumberFormatException e)
				{
					JOptionPane.showMessageDialog(null,"Please enter a 1, 2, 3, or 4.", "Error", JOptionPane.INFORMATION_MESSAGE);
				}
			}
			return housing;
	}


	//the finish() method successfully terminates the application
	public static void finish()
	{
		System.exit(0);
	} //end finish()


} //end class

I am getting an error that says...
H:\JAVA\PetAdvice.java:88: variable pet might not have been initialized
return pet;

i think it is in...

//declare method variables

				String pet;

Is there some kind of way to initialize a string if this is the problem

Java is a "safe" language and not a very trusting one. Even if you have coded it in such a way so as to make sure that "pet" is always initialized before being returned, Java still requires that there be pretty much no possible way that such a return situation can happen. C++ might issue you a warning, which you are free to ignore, but Java doesn't give you that option. So you must initialize it to SOMETHING in your program. You are going to make some assignment in the code that overwrites that initialization so it's irrelevant what value you initialize it to, but you still need that initialization to get it to compile. So just initialize it to whatever:

String pet = "Hello world";

or simply the empty string:

String pet = "";

and see if it compiles and does what you want.

Member Avatar for whoost

VernonDozier is correct as to why your program won't compile.

This however, is not why I am commenting, as it has already been stated. I am here to state that once you do said corrections, you will need modifications to your switch statement, which is what i thought was the original cause of your problem.

to state:

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

will not achieve the results you want (just try to get the result "dog"). since you are passing housing as the argument to the switch , saying case 1 is like saying "if housing equals 1," then do something. To do what you want, instead, you should say:

switch(housing)
	{
		case 1:
		if (hours>=18){
			pet = "pot-bellied pig";
		}
		else if ((hours>=10) && (hours < 18)){
			pet = "dog";
		}
		else if (hours < 10){
			pet = "snake";
		}
			break;

Seeing as how this is an assignment, i would suggest that you study (perhaps even try) both ways, and see if you can get the rest of the switch statement. That'd be the best way to learn, but theres always people to help if you still get stuck. Hope this sets you on the path to get the results you want.

peace

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.