jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes I useDelimiter \t(ab) with my Scanner, but it splits around spaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I useDelimiter \t(ab) with my Scanner, but it splits around spaces" Watch "I useDelimiter \t(ab) with my Scanner, but it splits around spaces" New topic
Author

I useDelimiter \t(ab) with my Scanner, but it splits around spaces

Simone Cittadini
Greenhorn

Joined: Apr 28, 2006
Posts: 13
I have to parse files like this :

0093Afghanistan0,1775
009370Afghanistan Mobile0,1789
009379 [tab] Afghanistan [space] Mobile [space] Roshan [tab] 0,1789

so I use code like this :



but I get as output :

0093
Afghanistan
0,1775
009370
Afghanistan
Mobile
0,1789
009379
Afghanistan
Mobile
Roshan
0,1789

instead of what I want :

0093
Afghanistan
0,1775
009370
Afghanistan Mobile
0,1789
009379
Afghanistan Mobile Roshan
0,1789

Where I do wrong ?
Nick Meverden
Ranch Hand

Joined: Jul 03, 2006
Posts: 59
String test = "this" + '\u0009' + "is a" + '\u0009' + "test";
java.util.regex.Matcher m = java.util.regex.Pattern.compile("^(.+)\t(.+)\t(.+)$").matcher(test);
boolean matches = m.matches();
if (matches) {
System.out.println("1 " + m.group(1));
System.out.println("2 " + m.group(2));
System.out.println("3 " + m.group(3));
}
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
Simone Cittadini
Greenhorn

Joined: Apr 28, 2006
Posts: 13
ok, I was not understanding how Scanner works, thought the default was to find carriage returns in a text file. Looked what was happening with the debugger and came out with this :

 
Consider Paul's rocket mass heater.
 
subject: I useDelimiter \t(ab) with my Scanner, but it splits around spaces
 
Similar Threads
Reading a tab delimited txt file and replacing blanks with zeores.
InputStream to fileReader
[newbie] String.replace()/replaceAll() removed spaces
Compare Two Text Files
InputStreamReader