aspose file tools*
The moose likes Java in General and the fly likes switch case Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "switch case " Watch "switch case " New topic
Author

switch case

nitss bhavsar
Ranch Hand

Joined: Jan 09, 2012
Posts: 55
hello, i have to convert if else in switch case ...
my code is




reply,how to do this?

Edit: added code tags
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
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

Joined: Jan 09, 2012
Posts: 55
hey,thanks for your reply..so how do i modify my code to support switch case?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4464
    
    8

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

Joined: Jan 09, 2012
Posts: 55
I have to use switch because my code contains multiple if else statements...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
nitss bhavsar wrote:I have to use switch because my code contains multiple if else statements...
No, you don’t.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4464
    
    8

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.
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

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.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11477
    
  16

well...you could do this:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

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

Joined: Jan 09, 2012
Posts: 55
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
Bartender

Joined: May 31, 2011
Posts: 1776
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
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Rob Spoor wrote: . . . Challenge accepted. . . .
Damn!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: switch case