[英]Create a string arraylist from another string arraylist by splitting strings
I need to create a String ArrayList from another String ArrayList by splitting the strings from the first one into the second one. 我需要通过将字符串从第一个拆分为第二个来从另一个String ArrayList创建一个String ArrayList。 The data comes from a .txt file being read in by a BufferedReader.
数据来自BufferedReader读取的.txt文件。 This app is supposed to display a spinner from which the user can choose a student's name then calculate their grade and display that as well.
该应用程序应该显示一个微调框,用户可以从中选择一个学生的姓名,然后计算其成绩并显示该成绩。 The biggest hurdle right now is that I get a null pointer exception when I try to use .split.
现在最大的障碍是,当我尝试使用.split时,出现空指针异常。 As you can probably tell, I'm fairly new to this and could use some help.
您可能会说,我对此还很陌生,可以使用一些帮助。 Thanks!
谢谢!
grades.txt 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 code 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();
}}
The biggest hurdle right now is that I get a null pointer exception when I try to use .split
现在最大的障碍是,当我尝试使用.split时,出现空指针异常
You are running while (line != null){
您正在跑步
while (line != null){
Which means that in-order for this loop to stop, line
will be null
. 这意味着,为了使此循环停止,
line
将为null
。
Let's assume this is a typo.... 让我们假设这是一个错字。
ArrayList<String> finalList = finalList = new ArrayList<String>();
Anyways, you can Arrays.asList(line.split("\\\\s+")
to get a list, which you can addAll
into the finalList
. 无论如何,您可以
Arrays.asList(line.split("\\\\s+")
获得一个列表,您可以将addAll
到finalList
。
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();
}
Though, realistically, I don't think this is what you want. 虽然,实际上,我认为这不是您想要的。 because
因为
This app is supposed to display a spinner from which the user can choose a student's name then calculate their grade and display that as well
该应用程序应该显示一个微调器,用户可以从中选择一个学生的姓名,然后计算其成绩并显示该成绩
Each line should be parsed into a Student
object, probably, then finalList
can be List<Student>
每行应该被解析为一个
Student
对象,那么finalList
可以是List<Student>
public class Student {
String firstName, lastName;
int test1, test2, test3, final; // or List<Integer> tests;
}
Then you need an ArrayAdapter<Student>
that you can customize to display only the name of the student. 然后,您需要一个
ArrayAdapter<Student>
,您可以对其进行自定义以仅显示学生的姓名。
Next, you need an item selection listener on the Spinner to calculate and display the grades on the Activity. 接下来,您需要在Spinner上使用项目选择侦听器来计算和显示Activity上的成绩。
These are separate issues you should post new questions for, though. 这些是单独的问题,不过您应该针对这些问题发布新的问题。
Then, you have Spinner spinner = new Spinner(this);
然后,您有了
Spinner spinner = new Spinner(this);
, but this Spinner is not coming from your XML layout... maybe you should use findViewById
to get the Spinner that is ,但是此Spinner并非来自您的XML布局...也许您应该使用
findViewById
来获得
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.