This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

switch case  RSS feed

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, i have to convert if else in switch case ...
my code is




reply,how to do this?

Edit: added code tags
 
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The switch statement does not work with boolean and hence you cannot have relational operators in the case statements.

Read the tutorials - Oracle switch statement tutorial



Please UseCodeTags next time.
 
Marshal
Posts: 60083
188
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lots of instanceofs? That’s a sure sign of bad design. You ought to use generics. And instanceof Object? I challenge you to show any occasion where you can retrieve something from a collection and instanceof Object returns false

Are you aware of the signum() method? You can enter the difference between the two numbers and get a return value. You can do simple arithmetic with that return value and use it as the index for your array. You can then dispense with the if-else, switch-case or whatever.
 
nitss bhavsar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,thanks for your reply..so how do i modify my code to support switch case?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

nitss bhavsar wrote:hey,thanks for your reply..so how do i modify my code to support switch case?


You can't, as John says, because you haven't got a suitable variable to switch on. Not without taking a fundamentally different approach to whatever you're trying to do. Why do you have to use a switch?
 
nitss bhavsar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to use switch because my code contains multiple if else statements...
 
Campbell Ritchie
Marshal
Posts: 60083
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

nitss bhavsar wrote:I have to use switch because my code contains multiple if else statements...

No, you don’t.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes a set of if/else statements can be usefully converted to a switch statement. But only if they are the right sort of conditions. Switch statements work on the value of a single variable (an integer, enum, or - as of Java 7 - String), whereas if statements can handle any boolean condition. So it's not always possible, or sensible, to try and crowbar in a switch statement.
 
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the question is about switch statements and you got a pretty clear answer that switch is not appropriate here, but maybe if you explained where input1 and input2 came from, it would become apparent how to change the code so you could avoid all the instanceof operators and maybe reduce the number of if statements.
 
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well...you could do this:

 
Sheriff
Posts: 21452
95
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:And instanceof Object? I challenge you to show any occasion where you can retrieve something from a collection and instanceof Object returns false


Challenge accepted. Just put the following class inside the same package:
 
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:Challenge accepted. Just put the following class inside the same package:


Today I've read in newspapers that the European Union wants to regulate hairdressers, so that they don't accidentally slay customers after stumbling in their stilettos (so no stilettos and no wedding rings).

Rob, beware of the day Brussels decides to regulate Java developers. You'll be first in line

Disclaimer: don't know how reliable the news I mentioned actually is -- our journalists are pretty inventive sometimes.
 
nitss bhavsar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey thanks all of you for your help...my problem is resolved..i have made following changes


public void invoke(final SplitContext c)
{
if((input1 instanceof Integer && input2 instanceof Integer))
{

// integer1=(Integer)input1;
// integer2=(Integer)input2;

final int IS_INTEGER = 0, IS_OBJECT = 1;
int instanceType=-1;

if(input1 instanceof Integer && input2 instanceof Integer)
instanceType = IS_INTEGER;
else if(input1 instanceof Object || input2 instanceof Object)
instanceType = IS_OBJECT;

switch (instanceType) {
case IS_INTEGER:

if((integer1.intValue()<integer2.intValue()))
{
SplitActivation.Activate(TRANSITIONS[0], c);
}

else if((integer1.intValue()==integer2.intValue()))
{
SplitActivation.Activate(TRANSITIONS[1], c);
}

else
{
SplitActivation.Activate(TRANSITIONS[2], c);

}


break;
case IS_OBJECT:
if (input1.equals(input2))
{
SplitActivation.Activate(TRANSITIONS[1], c);

}
else
SplitActivation.Activate(TRANSITIONS[3], c);


break;
default:SplitActivation.Activate(TRANSITIONS[3], c);



}






}
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of marking it in a int variable and doing a switch over it, you could have written your processing logic in the if and else if block itself.

Also please UseCodeTags.
 
Campbell Ritchie
Marshal
Posts: 60083
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote: . . . Challenge accepted. . . .

Damn!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!