我需要通过将字符串从第一个字符串拆分为第二个字符串,从另一个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 89java代码
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(); }}I need to create a String ArrayList from another String ArrayList by splitting the strings from the first one into the second one. The data comes from a .txt file being read in by a BufferedReader. 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. As you can probably tell, I'm fairly new to this and could use some help. Thanks!
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 89java code
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+")来获取列表,您可以将其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(); }但实际上,我认为这不是你想要的。 因为
这个应用程序应该显示一个微调器,用户可以从中选择学生的名字,然后计算他们的成绩并显示它
应该将每一行解析为Student对象,然后finalList可以是List<Student>
public class Student { String firstName, lastName; int test1, test2, test3, final; // or List<Integer> tests; }然后,您需要一个ArrayAdapter<Student> ,您可以自定义它以仅显示学生的姓名。
接下来,您需要Spinner上的项目选择侦听器来计算和显示活动上的成绩。
不过,这些是您应该发布新问题的单独问题。
然后,你有Spinner spinner = new Spinner(this); ,但是这个Spinner并不是来自你的XML布局...也许你应该使用findViewById来获取Spinner
The biggest hurdle right now is that I get a null pointer exception when I try to use .split
You are running while (line != null){
Which means that in-order for this loop to stop, line will be 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.
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>
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.
Next, you need an item selection listener on the Spinner to calculate and display the grades on the Activity.
These are separate issues you should post new questions for, though.
Then, you have 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
更多推荐
发布评论