IntelliJ: Generate switch case

JavaIntellij Idea

Java Problem Overview


Is there really no way to generate a switch case for a given variable in IntelliJ?

Ctrl+Space as well as Ctrl+J yield no results.

Java Solutions


Solution 1 - Java

For enum variables, enter switch (myEnumVar) and press Alt+Enter. Smart completion will suggest: Create missing 'switch' branches

Crazy Coder provided the following screenshot showing how to enable the Create Enum Switch Branches intention.

Intention: Create Enum Switch Branches

See YouTrack issue 6374.

Solution 2 - Java

Follow the steps here: https://confluence.jetbrains.com/display/PYH/Creating+and+applying+live+templates+(code+snippets)

for the context, use JAVA and the code for a switch - case Statement would be:

switch($switchVar$) {
case $value$:
    $END$
    break;
default:
    break;
}

Do the same for try - catch ;)

try {
$END$
} catch(Exception e) {e.printStackTrace();}

I love the template function in intellij and use it a lot.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStefan HothView Question on Stackoverflow
Solution 1 - JavaChristopher PeisertView Answer on Stackoverflow
Solution 2 - JavaFabian KnappView Answer on Stackoverflow