0

Hi i am doing a program which allows a user to insert paragraphs of data into a scanner input and i have a loop to check the next line if its blank to break but i want it to check the next 2-3 lines for text because it auto exits when paragraphs

The code:

    String tex ="";
    String line;
    while (in.hasNextLine()) {
        line = in.nextLine();
        if (line.isEmpty()) {
            break;
        }
        tex += line + "\n";
    }

Would i just add another if statement? or is there a more efficient way to get the desired output i am looking for?. i want to be able to post a paragraph or two into the scanner (im using a scanner for a bigger purpose) without it pre-terminating.

Jualston
  • 37
  • 6

2 Answers2

2

Here is the solution to your problem.

Count the number of times that the condition isBlank() is TRUE consecutively and BREAK if maximum accepted.

public static void main(String[] args) {

    final var MAXIMUM_LINE_TESTED = 2;
    var counterTest = 0;

    try (var sc = new Scanner(System.in)) {

        final var al = new ArrayList<String>();

        System.out.println("Enter text:");
        while (true) {
            final var line = sc.nextLine();
            if (line.isBlank()) {
                counterTest++;
                if (counterTest >= MAXIMUM_LINE_TESTED)
                    break;
            } else {
                counterTest = 0;
            }
            al.add(line);
        }

        for (final String v : al) {
            System.out.println(v);
        }

    }
}

Before update 26.02.21 :

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

The break statement can also be used to jump out of a loop.

by w3schools


You must use CONTINUE so as not to exit the WHILE. BREAK stops the WHILE if the condition is true.

CONTINUE:

public static void main(String[] args) throws Exception {
    String s = "hello world!hello world!hello world!hello world!hello world!\n" 
            + " \n"
            + "hello world!hello world!hello world!hello world!hello world!\n"
            + "hello world!hello world!hello world!hello world!hello world!\n"
            + "hello world!hello world!hello world!hello world!hello world!\n";
    try (Scanner scanner = new Scanner(s)) {
        String line = "", text = "";
        while (scanner.hasNextLine()) {
            line = scanner.nextLine();
            if (line.isBlank()) {
                System.out.println("skipped");
                continue;
            }
            text += line + "\n";
        }
        System.out.println(text);
    }
}

BREAK:

public static void main(String[] args) throws Exception {
    String s = "hello world!hello world!hello world!hello world!hello world!\n" 
            + " \n"
            + "hello world!hello world!hello world!hello world!hello world!\n"
            + "hello world!hello world!hello world!hello world!hello world!\n"
            + "hello world!hello world!hello world!hello world!hello world!\n";
    try (Scanner scanner = new Scanner(s)) {
        String line = "", text = "";
        while (scanner.hasNextLine()) {
            line = scanner.nextLine();
            if (line.isBlank()) {
                System.out.println("skipped");
                break;
            }
            text += line + "\n";
        }
        System.out.println(text);
    }
}
JTorleon
  • 108
  • 1
  • 9
  • Doesnt the first example one infinitely continue and the second one just infinitely put skipped? i need it to allow paragraphs but i also need it to stop at the end as well. – Jualston Feb 25 '21 at 13:57
  • if using an input of text typed it the false wouldnt occur considering it uses the if line.isblank it just adds skips and goes to the next line over and over again – Jualston Feb 25 '21 at 17:46
  • True, Count the number of times that the condition isBlank() is TRUE consecutively and BREAK if maximum accepted. I add an example to the answer. – JTorleon Feb 26 '21 at 04:04
0

After several hours searching for solutions, this is how i was able to resolve the issue. To prevent scanner.nextLine(); from exiting while loop, You need to duplicate it. Add another Scanner.nextLine(); to the end of the while loop.

String tex ="";
String line;
while (true) {
    line = in.nextLine();
    if (line.isEmpty()) {
        break;
    }
    tex += line + "\n";
    in.nextLine()
}
Ismail Osunlana
  • 434
  • 5
  • 9