aspose file tools*
The moose likes Beginning Java and the fly likes Splitting words Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Splitting words" Watch "Splitting words" New topic
Author

Splitting words

Josh Mark
Greenhorn

Joined: Mar 12, 2013
Posts: 2
Les Miserables,Tom Hooper,7.9
Avengers,Joss Whedon,9.0

so i have these words on a text file and i want to split them to different parts such that it will be
Les Miserables
Tom Hooper
7.9

and here is my code

Scanner in = new Scanner(files).useDelimiter("\\,");

String x = in.next();
String y = in.next();
String z = in.next();

System.out.println(x);
System.out.println(y);
System.out.println(z);


but the result that comes out is
Les Miserables
Tom Hooper
7.9
Avengers


what should i do to remove the Avengers??
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

My GUESS is that since you are using the comma as a delimiter, it is reading until it finds a comma. so really, your variable z is being set to everything between the 2nd and 3rd comma - including the newline.

What you need to do is read the file a line at a time, and then break each of those lines apart.

-or-

it may be possible (i really don't know) to set a comma OR a newline as a delimiter. I'd suggest reading the API.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
And welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Splitting words