簡體   English   中英

通過拆分字符串從另一個字符串數組列表創建一個字符串數組列表

[英]Create a string arraylist from another string arraylist by splitting strings

我需要通過將字符串從第一個拆分為第二個來從另一個String ArrayList創建一個String ArrayList。 數據來自BufferedReader讀取的.txt文件。 該應用程序應該顯示一個微調框,用戶可以從中選擇一個學生的姓名,然后計算其成績並顯示該成績。 現在最大的障礙是,當我嘗試使用.split時,出現空指針異常。 您可能會說,我對此還很陌生,可以使用一些幫助。 謝謝!

grades.txt

Name            Test1   Test2   Test3   Final
Adam    Anderson    81  90  85  87
Ben Brown       77  80  68  94
Chris   Cross       74  80  56  62
Don Dare        86  94  90  89
Eric    Earl        96  93  90  98
Fred    Foley       79  92  59  86
Gina    Gray        80  83  95  87
Holly   Hank        74  77  75  78
Ian Ingram      66  64  56  60
Jill    Johnson     90  98  78  89      

Java代碼

    public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String strName, strLastName, strFirstName, strFinalGrade;

    int intTest1, intTest2, intTest3, intFinal, intFinalGrade;

    int intPointsPossible = 400;

    int finalGrades[] = new int[0];

    AssetManager am = getAssets();
    ArrayList<String> list = new ArrayList<String>();
    ArrayList<String> finalList = finalList = new ArrayList<String>();
            BufferedReader reader;
    String line = " ";
    String item = " ";

    try {
        InputStream input = am.open("grades.txt");
        reader = new BufferedReader(new InputStreamReader(input));

        while (line != null){
            line = reader.readLine();
            list.add(line);

        }
        while (item != null){
            for (int i = 0; i < list.size(); i++){
                line.split("\\s+");
                finalList.add(item);
            }
        }

        input.close();

    } catch (IOException e) {
        e.printStackTrace();
    }




    Spinner spinner = new Spinner(this);

    ArrayAdapter<String> dataAdapter =
            new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);

    spinner.setAdapter(dataAdapter);

    dataAdapter.notifyDataSetChanged();

}}

現在最大的障礙是,當我嘗試使用.split時,出現空指針異常

您正在跑步while (line != null){

這意味着,為了使此循環停止, line將為null

讓我們假設這是一個錯字。

ArrayList<String> finalList = finalList = new ArrayList<String>();

無論如何,您可以Arrays.asList(line.split("\\\\s+")獲得一個列表,您可以將addAllfinalList

BufferedReader reader;

try {
    InputStream input = am.open("grades.txt");
    reader = new BufferedReader(new InputStreamReader(input));
    String line;

    while ((line = reader.readLine()) != null) {
        String[] parts = line.split("\\s+");
        finalList.addAll(Arrays.asList(parts));
    }

    input.close();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (reader != null) reader.close();
}

雖然,實際上,我認為這不是您想要的。 因為

該應用程序應該顯示一個微調器,用戶可以從中選擇一個學生的姓名,然后計算其成績並顯示該成績

每行應該被解析為一個Student對象,那么finalList可以是List<Student>

public class Student {
    String firstName, lastName;
    int test1, test2, test3, final; // or List<Integer> tests;
}

然后,您需要一個ArrayAdapter<Student> ,您可以對其進行自定義以僅顯示學生的姓名。

接下來,您需要在Spinner上使用項目選擇偵聽器來計算和顯示Activity上的成績。

這些是單獨的問題,不過您應該針對這些問題發布新的問題。


然后,您有了Spinner spinner = new Spinner(this); ,但是此Spinner並非來自您的XML布局...也許您應該使用findViewById來獲得

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM