I'm trying to build an app that will read a text file , then store each line of text as an array list.
this is my text file:
1 , Where is the white house? , Paris , Amsterdam , New York , Washington 2 , The Sopranos Is a..? , Italian Food , Tv series , Kind of Knife , A Book 3 , The Capital City Of Brazil is? , Rio de Janeiro, Amsterdam , Brazilia , Washington 4 ,Who Invanted The Phone ?, Alexander Graham Bell, Albert Einstein , Pinokio , Snoop Doog i'm basically trying to build an trivia app that will choose each line from the text file, then split the selected line into string array ,and finally print on the screen one question and four answers.
this is my code so far:
public class QuestionSql extends Activity { private String[] value; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.highscore); readFile(); } private void readFile() { // TODO Auto-generated method stub AssetManager manger; String line = null; try { manger = getAssets(); InputStream is = manger.open("text.txt"); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); while ((line = br.readLine()) != null) { value = line.split(","); //System.out.print(value); } br.close(); } catch (IOException e1) { System.out.println("not good"); } } } the problem is that the app only print the last line of the text file
thank you for the answers, it really helped me! this is my code so far:
public class QuestionSql extends Activity {
private String[] value; private List<String[]> collection; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.highscore); readFile(); convertListToString() } private void convertListToString() { value = collection.toArray(new String[collection.size()]); } private void readFile() { // TODO Auto-generated method stub AssetManager manger; String line = null; collection = new ArrayList<String[]>(); try { manger = getAssets(); InputStream is = manger.open("text.txt"); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); while ((line = br.readLine()) != null) { value = line.split(","); collection.add(value); } br.close(); } catch (IOException e1) { System.out.println("not good"); } } }
now, i need to convert the :collection = new ArrayList(); into string[] so i could set the text on my app buttons. any ideas?