Results 1 to 3 of 3
  1. #1
    jimJohnson is offline Member
    Join Date
    Jan 2008
    Posts
    22
    Rep Power
    0

    Default problem with operator in case

    Ok I really am stuck on this problem. It is in my case switch and been trying to figure this error out all morning and was wanting to know if anyone would be able to help me with it.

    The errors are:

    F:\JAVA\PetAdvice.java:55: operator && cannot be applied to int,boolean
    if ((housing = 1) && (hours>=18))
    ^
    F:\JAVA\PetAdvice.java:59: operator && cannot be applied to int,boolean
    if ((housing = 1) && (hours>=10) && (hours < 18))
    ^
    F:\JAVA\PetAdvice.java:63: operator && cannot be applied to int,boolean
    if ((housing = 1) && (hours < 10))
    ^
    F:\JAVA\PetAdvice.java:67: operator && cannot be applied to int,boolean
    if ((housing = 2) && (hours >= 10))
    ^
    F:\JAVA\PetAdvice.java:71: operator && cannot be applied to int,boolean
    if ((housing = 2) && (hours < 10))
    ^
    F:\JAVA\PetAdvice.java:75: operator && cannot be applied to int,boolean
    if ((housing = 3) && (hours >= 6))
    ^
    F:\JAVA\PetAdvice.java:79: operator && cannot be applied to int,boolean
    if ((housing = 3) && (hours < 6))
    ^


    I know that it has to do something with the && but everything looks right to me well as good as a beginner would think it would and going to post my code and if anyone has an idea please shoot

    Java Code:
    /*
    
    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

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    if ((housing = 1) && (hours>=18))
    housing = 1 is an assignment:
    It assigns the value of one (1) to the "housing" variable.
    To make this statement a boolean use the "==" operator:
    Java Code:
    if ((housing == 1) && (hours >= 18))
    // or
    if (housing == 1 && hours >= 18)

  3. #3
    jimJohnson is offline Member
    Join Date
    Jan 2008
    Posts
    22
    Rep Power
    0

    Default

    haha god damnit I should have known that thanks man appreciate it

Similar Threads

  1. String Title case
    By bugger in forum New To Java
    Replies: 6
    Last Post: 01-31-2012, 01:21 PM
  2. How to convert a String to upper case
    By Valeriano in forum New To Java
    Replies: 16
    Last Post: 03-01-2010, 12:39 PM
  3. Case Based Reasoning
    By kbyrne in forum Advanced Java
    Replies: 4
    Last Post: 04-12-2008, 08:51 PM
  4. Method/Operator Overloading
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 08:33 PM
  5. Can I set a range in case statement?
    By christina in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:41 PM

Posting Permissions

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