How to load random line from text file in android? -
i have piece of code;
scanner s = new scanner(getresources().openrawresource(r.raw.game)); try { while (s.hasnextline()) { system.out.println(s.nextline()); } } { s.close(); }
how can make load random line piece of code?
thanks.
you load lines data structure such arraylist
, use random
generate random index number.
here's code put arraylist:
scanner s = new scanner(getresources().openrawresource(r.raw.game)); arraylist<string> list = new arraylist<string>(); try { while (s.hasnextline()) { list.add(s.nextline()); } } { s.close(); }
this code return random line:
public static string randomline(arraylist list) { return list.get(new random().nextint(list.size())); }
Comments
Post a Comment