View Single Post
  #1 (permalink)  
Old 03-21-2008, 09:29 PM
jimJohnson jimJohnson is offline
Member
 
Join Date: Jan 2008
Posts: 18
jimJohnson is on a distinguished road
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

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
Reply With Quote
Sponsored Links