java - how to retrieve a line with spaces with comma as separator? -
i have record in game.txt file
menard,menard mabunga,0 francis,francis mabunga,0 angelica,francis mabunga,1
i access file , store in array list using code;
scanner s = new scanner(getresources().openrawresource(r.raw.game)); arraylist<string> list = new arraylist<string>(); while (s.hasnext()){ list.add(s.next()); } s.close();
and use function load random line;
public static string randomline(arraylist list) { return (string) list.get(new random().nextint(list.size())); }
when try print result of randomline function using system.out.println(randomline(list));
output mabunga,0
only.
now, how can retrieve line spaces comma separator?
you reading words instead of lines. use nextline()
instead of next()
. so, should code:
scanner s = new scanner(getresources().openrawresource(r.raw.game)); arraylist<string> list = new arraylist<string>(); while (s.hasnext()){ list.add(s.nextline()); // change here!! } s.close();
i prefer bufferedreader
on scanner
if goal read lines.
Comments
Post a Comment