Jualston
Jualston

Reputation: 39

Scanner not reading the next line over and exiting loop

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.

Upvotes: 0

Views: 953

Answers (2)

Ismail Osunlana
Ismail Osunlana

Reputation: 434

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()
}

Upvotes: 0

Tom
Tom

Reputation: 170

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);
    }
}

Upvotes: 2

Related Questions