简体   繁体   English

Java应用程序-从文本文件读取值,添加到arraylist

[英]Java application - Read values from text file, add to arraylist

I'm working on a multiple choice quiz application in Android Studio, and inside a text file I have the answers to each question, as well as their corresponding questions. 我正在使用Android Studio中的多项选择测验应用程序,并且在一个文本文件中,我可以找到每个问题的答案以及相应的问题。 This is my first attempt at such a task, so apologies in advance for code that may be considered sloppy. 这是我第一次尝试执行此任务,因此对可能被认为草率的代码提前致歉。

This is what I have for my text file: 这是我的文本文件的内容:

Java, What language is most commonly used for Android application development? Java,哪种语言最常用于Android应用程序开发?

Android Studio/Eclipse, Which of these programs is often used for Android application development? Android Studio / Eclipse,以下哪个程序通常用于Android应用程序开发?

2005, In what year did Google acquire Android? 2005年,Google在哪一年收购了Android?

(There are several more, but this should get the point across.) (还有其他几种,但这应该可以理解。)

The first string before the comma is what I'm trying to add to arrayListTerms, and the string following the comma to arrayListDef. 逗号之前的第一个字符串是我要添加到arrayListTerms的字符串,逗号后面的字符串是添加到arrayListDef的字符串。 Here's my code so far: 到目前为止,这是我的代码:

    public class ActivityTwo extends AppCompatActivity {
int sizeOfArray;
String[][] qAndA;//Stores Questions and Answers
String[] temp;
ArrayList<String>arrayListTerms;
ArrayList<String>arrayListDef;
HashMap<String, String> qMatch = new HashMap<>();//Matches correct answer with current question
public static final String TAG =" ";

public ActivityTwo() {
    arrayListDef = new ArrayList<>();
    arrayListTerms = new ArrayList<>();
}

@Override
protected void onCreate(Bundle savedInstanceState) {

    try {
        InputStream myInputStream = this.getResources().openRawResource(R.raw.textfile);
        BufferedReader myBufferedReader = new BufferedReader(new InputStreamReader(myInputStream));
        String str = myBufferedReader.readLine();

        while((str != null)) {
            temp = str.split(",");
            arrayListTerms.add(temp[0]);
            arrayListDef.add(temp[1]);
            //qMatch.put(temp[1], temp[0]);

        }
        Log.i(TAG, "Textfile Loaded.");
        myBufferedReader.close();
    } catch (FileNotFoundException ex) {
        Log.e(TAG, "Unable to open text file.");
    } catch (IOException ex){
        Log.e(TAG, "Error reading text file.");
}

    Collections.shuffle(arrayListDef);
    sizeOfArray = arrayListDef.size();

尝试使用file.io读取文件,将其指向该位置,然后应该可以将输入的内容更新到数组列表中。

Change 更改

while((str != null)) {
            temp = str.split(",");
            arrayListTerms.add(temp[0]);
            arrayListDef.add(temp[1]);
            //qMatch.put(temp[1], temp[0]);

        }

To

String line = "";
    while((line = str) != null) {
                temp = line.split(",");
                arrayListTerms.add(temp[0]);
                arrayListDef.add(temp[1].trim()); //eliminate space after ','
                //qMatch.put(temp[1], temp[0]);

            }

You need to initialize an empty string then fill it in the while condition. 您需要初始化一个空字符串,然后将其填充在while条件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM