Multiple delimiters in Scanner class of Java

tomejuan picture tomejuan · Feb 4, 2011 · Viewed 60.9k times · Source

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.

Answer

Jigar Joshi picture Jigar Joshi · Feb 4, 2011
 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