Reputation: 79
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
Reputation: 48297
You can set the delimiter to that object...
Scanner in = new Scanner(System.in);
in.useDelimiter("\n\n");
Upvotes: 2