This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Splitting words

 
Josh Mark
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12196
35
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50235
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic