Win a copy of Learn Spring Security (video course) this week in the Spring 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: 12083
29
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
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic