• Post Reply Bookmark Topic Watch Topic
  • New Topic

combining switch statements to if...else.  RSS feed

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can anyone offer me some guidance on combining switch statements into if...else decisions? I'm basically trying to teach myself java and am at the point where I have to combine two user inputted values into if...else statements, only I don't really grasp how to do so. If someone could point me in the right direction I'd appreciate it. (This stuff is soooo addictive.) I'm trying to let the user input the type of residence they have, how many hours they are usually home, and then recommend a pet based on that. I just don't get how to input both selections they make in if...then statements. I get a bunch of errors as soon as I start the if...else part.

Thanks for any responses.

 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There aren't any switch statements being "combined" into if-else statements there. You've just slipped up on the syntax of if-else a little bit. Here's a corrected version:


 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:There aren't any switch statements being "combined" into if-else statements there. You've just slipped up on the syntax of if-else a little bit. Here's a corrected version:




Geez, you people are wicked smart. It's really impressive. I still keep getting an error on the final one though. It says:
PetAdvice.java:78: error: bad operand types for binary operator '||'
else if (houseType == 1 && hourHome == 3 || 4 || 5)
^
first type: boolean
second type: int
1 error
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gob Bluth wrote:I still keep getting an error on the final one though. It says:
PetAdvice.java:78: error: bad operand types for binary operator '||'
else if (houseType == 1 && hourHome == 3 || 4 || 5)

Because:
if (houseType == 1 && hourHome == 3 || 4 || 5)
is not valid Java. I was going to suggest that you look at the tutorials, but in fact they don't show the format for a compound condition in the if section, so here it is: The ' || 4 || 5' is what is invalid.

The expression either side of a '||' or '&&' MUST be (or evaluate to) a boolean, so:
hourHome == 3 || hourHome == 4
is valid, but:
hourHome == 3 || 4
is not.

See if you can use that to correct your statement.

Winston
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can say, “If x is 3 or 4 or 5,” but you can't write it in Java®. It is “If x is 3 or x is 4 or x is 5,”
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You can say, “If x is 3 or 4 or 5,”...

You can say it, but it doesn't always make sense.

Cobol used to allow you to code "IF x NOT == 1 OR 2", which used to confuse smart beginners no end - it must be true, right?
Wrong.

The compiler actually converted it to "IF x NOT == 1 OR x == 2", which is not what people usually mean when they say "not = x or y"; all of which just added to the mess.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!