文件不会读入多个数组位置
我试图将文件中的每一行存储到一个数组列表中,然后将两个数组列表合并为一个。目前,当我尝试此操作时,所有不同的行都存储在一行中。我想让它说类似 User: Score 的内容。然而,现在它显示为 UseruserUsernamePerson : Score。 (许多不同的名称,只有一个分数)。有人能看出我哪里出错了吗?另外,请原谅我糟糕的命名习惯。我的数组列表以前是Vector,但我把它们改成了ArrayList,却忘了更改它们的标题。
public class DisplayScores extends ListActivity{
private ArrayList<String> scoreVector = new ArrayList<String>();
private ArrayList<String> userVector = new ArrayList<String>();
private ArrayList<String> comboVector = new ArrayList<String>();
private int c = 0;
File root = Environment.getExternalStorageDirectory();
File scores = new File(root, "scores.txt");
File users = new File(root, "names.txt");
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
String line = null;
try {
FileReader scoresFileReader = new FileReader(scores);
BufferedReader scoresReader = new BufferedReader(scoresFileReader);
while ((line = scoresReader.readLine())!= null)
{
scoreVector.add(line);
}
scoresFileReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String userLine = null;
try{
FileReader userFileReader = new FileReader(users);
BufferedReader userReader = new BufferedReader(userFileReader);
while((userLine = userReader.readLine())!= null)
{
userVector.add(userLine);
}
userReader.close();
} catch (IOException e){
e.printStackTrace();
}
for(String s : scoreVector)
{
comboVector.add(userVector.get(c) + ": " + s);
}
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, comboVector));
}
}
I'm trying to store each line from a file into an arraylist and then combine two arraylists into one. Currently, when I try this, all the different lines are being stored in one line. I want it to say something like User : Score . However, right now it is showing up like UseruserUsernamePerson : Score. (many different names and only one score). Can anyone see where I'm going wrong here? Also, pardon my poor naming practice. My array lists used to be Vectors, but I changed them into ArrayLists and forgot to change their titles.
public class DisplayScores extends ListActivity{
private ArrayList<String> scoreVector = new ArrayList<String>();
private ArrayList<String> userVector = new ArrayList<String>();
private ArrayList<String> comboVector = new ArrayList<String>();
private int c = 0;
File root = Environment.getExternalStorageDirectory();
File scores = new File(root, "scores.txt");
File users = new File(root, "names.txt");
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
String line = null;
try {
FileReader scoresFileReader = new FileReader(scores);
BufferedReader scoresReader = new BufferedReader(scoresFileReader);
while ((line = scoresReader.readLine())!= null)
{
scoreVector.add(line);
}
scoresFileReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String userLine = null;
try{
FileReader userFileReader = new FileReader(users);
BufferedReader userReader = new BufferedReader(userFileReader);
while((userLine = userReader.readLine())!= null)
{
userVector.add(userLine);
}
userReader.close();
} catch (IOException e){
e.printStackTrace();
}
for(String s : scoreVector)
{
comboVector.add(userVector.get(c) + ": " + s);
}
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, comboVector));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从代码来看,c 的值似乎没有增加。
c 始终为 0
from the code it seems that the value of c is not incrementing..
c is always 0