File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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
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: 10916
    
  12

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: 36508
    
  16
And welcome to the Ranch
 
wood burning stoves
 
subject: Splitting words
 
Similar Threads
Delimiter help please
Some of the most influential books
Issue about ArrayList
x++ , ++x
Making a binary to decimal converter