anuj kumar
anuj kumar

Reputation: 79

How to take multiline string input seperated by enter("\n") and ends with double enter ("\n\n) in java?

How to take multiline strings input (separated by enter (\n) and ends with (\n\n)) from console.

Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String[] tokens = scanner.nextLine().split("\\n");
            System.out.println(Arrays.toString(tokens));
        }

Example:- my name is xyz
i am from india
thats it
\n \n
(done after double enter)

output :
my name is xyz
i am from india
thats it

Upvotes: 1

Views: 67

Answers (2)

Varun Kumar N.R
Varun Kumar N.R

Reputation: 1

use Delimiter eg: in.useDelimiter("\n\n"); hope this works

Upvotes: 0

You can set the delimiter to that object...

Scanner in = new Scanner(System.in);
in.useDelimiter("\n\n");

Upvotes: 2

Related Questions