• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

problem with switch statement  RSS feed

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to figure out how to use this switch statement and I'm wondering if you can't use a string in it? Here's my code.
switch(legs) {
case 'one':
System.out.println("Poor dog, he can hardly get around. He's only got one leg.");
break;
case 'two':
System.out.println("Poor dog, he has to use a wagon for his back legs. Must be hard with only two legs.");
break;
case 'three':
System.out.println("Wow, it's not often you see a three legged dog!");
break;
default: System.out.println("Good, this dog isn't missing any limbs.");
}
And when I try to compile it, I get an error that points to the first apostrophe on the left of one ( case 'one' and it says Invalid Character Constant. Can I not test strings in a switch statement? My book has an example with single quotes around them, but it doesn't seem to be working. Any help would be greatly appreciated. Thanks!
 
tyler jones
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind, I guess I should have read a little further down instead of just looking at the example. lol Thanks anyway.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
1) have u defined varialble 'legs' ???
2) switch is used only for comparisions, is it valid to use switch in char variable ??
3) if yes, what comparision you are doing ??

pls think over this points ...
yash.

Originally posted by tyler jones:
I'm trying to figure out how to use this switch statement and I'm wondering if you can't use a string in it? Here's my code.
switch(legs) {
case 'one':
System.out.println("Poor dog, he can hardly get around. He's only got one leg.");
break;
case 'two':
System.out.println("Poor dog, he has to use a wagon for his back legs. Must be hard with only two legs.");
break;
case 'three':
System.out.println("Wow, it's not often you see a three legged dog!");
break;
default: System.out.println("Good, this dog isn't missing any limbs.");
}
And when I try to compile it, I get an error that points to the first apostrophe on the left of one ( case 'one' and it says Invalid Character Constant. Can I not test strings in a switch statement? My book has an example with single quotes around them, but it doesn't seem to be working. Any help would be greatly appreciated. Thanks!


 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
You can use only an int or int compatible value in a switch statemaent..instead of usin 'one', 'two'..use 1, 2.
It will work.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The cool way to do use strings with case statements:
static final int SANTA_CLAUSE = 1; //constants in all caps
static final int ANGRY_ELF = 2;
static final int ADVENTUROUS_REINDEER = 3;
String dealWithChristmasVisitor(int whatCameDownTheChimney) {
switch(whatCameDownTheChimney) {
case SANTA_CLAUSE: return "give cookies";
case ANGRY_ELF: return "threaten with mop";
case ADVENTUROUS_REINDEER: return "administer first aid";
}
}
Much easier on those trying to interpret your code....

[This message has been edited by eric moon (edited December 12, 2000).]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tyler,
It reveals from your post that u haven't selected a good book for Java. Switch only works for a integer expression or an expression which would yield to an integer.
Try more examples, keeping above statements in mind.
I'm sure, you'll be out of ur doubt
------------------
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!