0

I'm using Java with Eclipse.

In my application, one code snippet is there which is the long series of multiple if statements.

Recently I had to work upon that code & I found that these multiple if statements can be converted to if-else-if ladder.

Now converting this to if-else-if ladder manually is somewhat cumbersome.

Is there some short cut in Eclipse that I can use to do so?

RAS
  • 8,100
  • 16
  • 64
  • 86
  • 1
    This code sounds a bit messy. Have you considered refactoring it to remove some of the if statements? That might be a better approach for you and those who come after you. – Mark Chorley Jul 03 '13 at 10:05
  • 1
    I don't think there is. If copy/pasting an `else` in front of every of your `if` is considered too much work, there is something seriously wrong with your code ;-) – André Stannek Jul 03 '13 at 10:07
  • 2
    There is a big difference between series of `if` and an `if-else-if` ladder . Hope you know it ? It depends on logic what needs to be used . They are not equivalent . – AllTooSir Jul 03 '13 at 10:07
  • 1
    Well, consecutive `if` statements can't necessarily be replaced by `if-elseif` ones, and as this is a very specific need I doubt there's any functionality to do that. – Laurent S. Jul 03 '13 at 10:07
  • 4
    Use Find/Replace Dialog. Replace all `}\s*if` with `}else if`. Enable "Regular Expressions". You can select the lines you want and enable "Selected lines". – johnchen902 Jul 03 '13 at 10:08
  • @MarkChorley Yes it is messy. I'm refactoring it. And as a part of the refactoring, I need to convert it into if-else if ladder. – RAS Jul 03 '13 at 10:08
  • @TheNewIdiot Yes I know the difference. – RAS Jul 03 '13 at 10:10
  • @stonedsquirrel, there are 50 if statements. copy-paste is the last option I would prefer. – RAS Jul 03 '13 at 10:13
  • @RAS adding else ifs wouldn't be my first step under most circumstances. I think it's time you let the dog see the rabbit. Post your code – Mark Chorley Jul 03 '13 at 10:13
  • @johnchen902, can you please post it as answer? – RAS Jul 03 '13 at 10:57
  • @RAS Done. Also add a picture. – johnchen902 Jul 03 '13 at 11:04

4 Answers4

4
  • Use Find/Replace Dialog.

  • Replace all }\s*if with }else if.

  • Enable "Regular Expressions".

  • You can select the lines you want and enable "Selected lines".

enter image description here

johnchen902
  • 9,531
  • 1
  • 27
  • 69
4

After a long search I finally found the short cut in Eclipse.

I selected all the if statements, pressed Ctrl+1 (Right Click & selected Quick Fix).

It gave me an option "Join if sequence in if-else-if", which does exactly what I want.

RAS
  • 8,100
  • 16
  • 64
  • 86
  • Oh cool. I almost didn't see this answer, I'd suggest you un-accept the currently accepted answer and accept this one instead. – MikeFHay Jul 05 '13 at 16:04
  • @MikeFHay I know this is the actual answer I was looking for. But I've accepted john's answer as it was a nice answer when I had to solve the issue on urgent basis. This solution I've found later. So I think johnchen902's answer deserves the acceptance. – RAS Jul 09 '13 at 06:30
  • @RAS Your answer is better. Mine is just a hack and won't work if there are nested `if`. Please accept your answer instead. – johnchen902 Aug 05 '13 at 14:05
1

Find & Replace all "if" with "else if", then fix the compiler error on the first one by deleting the "else". Make sure you're not accidentally changing anything else that you shouldn't!

MikeFHay
  • 8,562
  • 4
  • 31
  • 52
  • Thanks for the answer, +1 from my side. But I think john's answer requires less manual effort. – RAS Jul 03 '13 at 11:22
1

Since it might not be the same logic, I don't think that Eclipse has that feature. I do know that IntelliJ have Inspections covering those kind of issues, so maybe a plugin doing code inspections can help you refactor.

Secondly, if it can be converted to if else if, then it might also be convertable to switch case depending on what variable you are looking at, and if it's the same. Also available on strings since java 7. It might also be refactorable to do switch on enums.

Viktor Mellgren
  • 4,318
  • 3
  • 42
  • 75
  • I cannot convert it to switch case because the operation is performed on String & I'm using Java 6. – RAS Jul 03 '13 at 10:35
  • You might also consider Enums – Viktor Mellgren Jul 03 '13 at 10:37
  • 1
    @RAS as Vixen suggests, if you are limited to a version before Java 7, you convert your Strings to Enums. The accepted answer [in this question](http://stackoverflow.com/questions/338206/switch-statement-with-strings-in-java) explains this rather well. – zEro Jul 03 '13 at 10:40
  • 1
    @zEro Ok I got the point. I'll convert the strings to enums as a part of code refactoring. – RAS Jul 03 '13 at 10:46
  • 1
    Thanks for the answer, +1 from my side. But I think john's answer requires less manual effort. – RAS Jul 03 '13 at 11:24
  • @RAS the effort might be worth it if the results give you better quality and easier maintainability of the code – Viktor Mellgren Jul 03 '13 at 11:33
  • 1
    @Vixen and your answer does that. I'll definitely use Enums & switch. – RAS Jul 03 '13 at 11:55