TransWikia.com

Java caret not going to the next line correctly in IntelliJ

Stack Overflow Asked on December 18, 2020

When I write a Switch statement in Java (I use IntelliJ IDEA), the blinking cursor, or caret, doesn’t go to the next line correctly. It’s not really a huge problem, but just an annoyance I’m not sure how to fix.

When I write a case for a switch code block:

switch(switchValue){
    case 1:
        System.out.println("Value was 1");
        break;
}

After the ‘break;’, when I enter the cursor stays inside the case 1 code block

switch(switchValue){
    case 1:
        System.out.println("Value was 1");
        break;
        <-- The cursor goes here
}

And when I backspace to move it back so I can make a new case, the line just goes back to the end of the break;

switch(switchValue){
    case 1:
        System.out.println("Value was 1");
        break;<-- cursor goes here
}

The only way I can move it to create a new case 2 is to manually use my directional keys 4 spaces after I press enter. Is there a keyboard shortcut where I can create a new case without the extra steps?

switch(switchValue){
    case 1:
        System.out.println("Value was 1");
        break;
    <-- cursor goes here and after I use the directional keys    
    case 2:
        System.out.println("Value was 2");
        break;
}

This was a really obscure minor issue I can’t seem to find the answer for.

One Answer

I think I fixed my own issue after posting the question.

If I just type "case 2:" after going to the next line after the "break;", IntelliJ IDEA will automatically format the case and move it to the correct position without any hassle.

switch(switchValue){
    case 1:
        System.out.println("Value was 1");
        break;
        case 2:
}

The moment you finish the case statement with the colon, the editor will automatically format the line and move the statement to the correct position.

switch(switchValue){
    case 1:
        System.out.println("Value was 1");
        break;
    case 2:
}

Correct answer by Danny on December 18, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP