File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Splitting words" Watch "Splitting words" New topic

Splitting words

Josh Mark

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

and here is my code

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

String x =;
String y =;
String z =;


but the result that comes out is
Les Miserables
Tom Hooper

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

Joined: Oct 02, 2003
Posts: 11952

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.


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

Joined: Oct 13, 2005
Posts: 46320
And welcome to the Ranch
I agree. Here's the link:
subject: Splitting words
It's not a secret anymore!