Results 1 to 3 of 3
Thread: problem with operator in case
- 03-21-2008, 07:29 PM #1
Member
- Join Date
- Jan 2008
- Posts
- 22
- Rep Power
- 0
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
- 03-21-2008, 08:05 PM #2
housing = 1 is an assignment:Java Code:if ((housing = 1) && (hours>=18))
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)
- 03-21-2008, 08:22 PM #3
Member
- Join Date
- Jan 2008
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
String Title case
By bugger in forum New To JavaReplies: 6Last Post: 01-31-2012, 01:21 PM -
How to convert a String to upper case
By Valeriano in forum New To JavaReplies: 16Last Post: 03-01-2010, 12:39 PM -
Case Based Reasoning
By kbyrne in forum Advanced JavaReplies: 4Last Post: 04-12-2008, 08:51 PM -
Method/Operator Overloading
By Java Tip in forum Java TipReplies: 0Last Post: 12-01-2007, 08:33 PM -
Can I set a range in case statement?
By christina in forum New To JavaReplies: 1Last Post: 07-25-2007, 08:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks