1

I'm trying to read multiple line input like a copy past input, but I can't end the while loop to stop reading. Here is my code:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input =null;
        while(sc.hasNext()) {
            input=sc.next();

        }
        String [] split = input.split(" ");
        for(int i = 0;i<split.length;i++) {
            for(int j = split[i].length()-1;j>=0;j--) {
                System.out.print(split[i].charAt(j));
            }
            System.out.print(" ");
        }
        sc.close();
    }

}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Bruno Cotrim
  • 57
  • 1
  • 6

3 Answers3

5

Use buffer reader class, by this you'll be able to take multiple inputs at the same time.

https://docs.oracle.com/javase/6/docs/api/java/io/BufferedReader.html

KaranManral
  • 644
  • 4
  • 13
2

The hasNext() method will look if there is something to read. To do so it might block and wait on the used InputStream as described in the documentation of hasNext():

public boolean hasNext()

Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input.

When the InputStream ends it will return from the hasNext() call and return false, since there is no token to read (and the source of data/bytes is closed). But since you are reading from the keyboard via System.in there is no "end" of input because you still can enter new text into your application, which your while() loop will read.

There are several solutions for this:

  1. Depending on how you start your java application, you can press CTRL-D to explicit close the so called "STDIN" stream which is "attached" to your keyboard input. That way the stream System.in will end and no further data can be read. This is also detected by the Scanners hasNext() method, which will return false at that point.

  2. You can call your java application where you provide the data for the "STDIN" stream via a file or via other commands. Examples of these are: echo "data" | java Main or java Main < inputData.txt (depending on the terminal/console you are using).

  3. Add an end marker to your content and look for it. When you place a text like "MY_TEXT_END" in your data and look for it you can use a simple equals() check inside the while() loop and stop reading when you have seen your marker "MY_TEXT_END".

Progman
  • 16,827
  • 6
  • 33
  • 48
1

Since you are using while loop and it will loop/execute through a block of code as long as a specified condition is true/met.

And here you are checking any condition to exit the loop.This method may block while waiting for input to scan and the scanner does not advance past any input.

Another gap that I see is, you are re-assigning the input to input var again and again without performing any operation beforehand accepting next line.

You can get more clarity on exiting the while loop here :

How to get out of while loop in java with Scanner method "hasNext" as condition?

    Scanner sc = new Scanner(System.in);
    String input = null;

    while(sc.hasNext() && !(input = sc.next()).equals("exit")) {
        System.out.println(input);
    }

    sc.close();

So, to answer your question, you must provide some check/exit condition for while loop to in-validate and exit out of the loop.

Kumar Ashutosh
  • 1,121
  • 10
  • 33
  • First of all thanks for the help, i have already tried it after all that initial code and at some point i got it but only to a unique text. – Bruno Cotrim Mar 01 '20 at 15:09
  • for example to exit the loop i used the number of lines or when it reaches an empty line the problem is that the text given is composed by this : (next comment) it has a blank line in the middle of the text like a double (Enter) – Bruno Cotrim Mar 01 '20 at 15:10