java - Read lines of a text file into an array -
i display new quote randomly every time launch app.in order thinking of using text file containing quotes, 1 @ each line.
i know in android studio should put text file , how can read text file , put each line array?
i found code looks make job doesn't seems work:
// set variables string filename = "myfriends.txt"; string line; arraylist alist = new arraylist(); //read lines of text arraylist try { bufferedreader input = new bufferedreader(new filereader(filename)); if (!input.ready()) { throw new ioexception() } while ((line = input.readline()) != null) { alist.add(line); } input.close(); } catch (ioexception e) { system.out.println(e); } thanks
solution: page helps lot : http://developer.android.com/guide/topics/resources/string-resource.html#stringarray
string[] quotes = getresources().getstringarray(r.array.quotes_array); random randomgenerator = new random(); //construct new random number generator int randomnumber = randomgenerator.nextint(quotes.length); fact = quotes[randomnumber];
that's pretty non-android way of thinking of things.
consider using res/values/strings.xml store quotes, read random 1 there using getresources().getstringarray(r.string.quotes), generating random integer between 0 , size of array.
bonus of using resources - can use same identifiers, change quotes based on region.
Comments
Post a Comment