Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple delimiters in Scanner class of Java

How do I use the useDelimiter() method of the Scanner class to use both the comma (,) and the new line character (\n) as delimiters?

I am parsing some text from a csv file.

like image 469
tomejuan Avatar asked Feb 04 '11 13:02

tomejuan


2 Answers

 Scanner s = new Scanner("hello, world \n hello world");
 s.useDelimiter(",|\\n");
 while(s.hasNext()){
          System.out.println(s.next());

 }

Output

hello
 world 
 hello world
  • JavaDoc
like image 178
jmj Avatar answered Sep 18 '22 15:09

jmj


How about useDelimiter(",|\\n");

like image 27
Andrew White Avatar answered Sep 20 '22 15:09

Andrew White