-1

I'm making a text editor in java. I'm using a switch to create a menu. One of the options of the menu is to insert lines to the existing array where i need to make a loop to add strings to an array and make it stop when i add a empty string. Note: i cannot use array lists. i can only use this array.

int tamMax=100;
String [] linhas = new String[tamMax];

This is the code i have so far

        System.out.println("(I)nserir linhas no fim (termine com uma linha vazia");
        System.out.println("(L)istar linhas");
        System.out.println("(A)pagar última linha");
        System.out.println("(E)ditar");
        System.out.println("(F)erramentas");
        System.out.println("(S)air");
        menuPrincipal = input.next().charAt(0);
        switch(menuPrincipal) {
            case 'i':
            case 'I':
                int i=0;
                while (true) {  
                    linhas[i] = input.nextLine();
                    System.out.println(linhas[i]);
                    if (linhas[i].isEmpty()) {
                        break;
                    }
                    i++;
                }
                System.out.print(Arrays.toString(linhas));

The loop itself is working fine, the problem is when i put the loop inside the switch case, once i do that and i press "i" the loop stops working, it doesn't let me type anything and only outputs the following result:[, null, null, null, null, null, null, null ...

Its_JC
  • 3
  • 2

1 Answers1

1

linhas[i] will always be null. You want to check the string that was inserted in the previous iteration (i - 1).

You can do the check within the for-loop body.

for (int i = 0; ; i++) {
    linhas[i] = input.nextLine();
    System.out.println(linhas[i]);
    
    if (linhas[i].isEmpty()) {
        break;
    }
}

If you don't want the empty string to be inserted, move the check before assignment.

String element = input.nextLine();
System.out.println(element);
if (element.isEmpty()) {
    break;
}
linhas[i] = element;
i++;

Since you do not know the number of times the loop will run, a while loop may be preferred.

while (true) {
    linhas[i] = input.nextLine();
    System.out.println(linhas[i]);
    if (linhas[i].isEmpty()) {
        break;
    }
    i++;
}
Thiyagu
  • 17,362
  • 5
  • 42
  • 79
  • Tried this one but the loop ends right away without even letting me type anything, only shows the following output [, null, null, null, null, null, null, null ... – Its_JC Dec 06 '22 at 07:10
  • You would have entered a newline (`\n`) as the first input – Thiyagu Dec 06 '22 at 07:12
  • before the cycle i'm using a switch to create a menu where i have to type "i" to enter the cycle, that's why it's not working, how can i make it work? – Its_JC Dec 06 '22 at 07:26
  • It's not entirely clear what you are doing. If you have an additional `\n`, use an additional `input.nextLine()` to clear it. I suggest posting a new question if there are changes in your code. – Thiyagu Dec 06 '22 at 07:30
  • i added the entire code so you could see now, they don't let me create a new question :( – Its_JC Dec 06 '22 at 07:55
  • One way to fix it - Replace `input.next().charAt(0)` with `input.nextLine().charAt(0)` – Thiyagu Dec 06 '22 at 08:04