文件不会读入多个数组位置

发布于 2024-11-06 19:32:53 字数 1838 浏览 4 评论 0原文

我试图将文件中的每一行存储到一个数组列表中,然后将两个数组列表合并为一个。目前,当我尝试此操作时,所有不同的行都存储在一行中。我想让它说类似 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚如初 2024-11-13 19:32:53

从代码来看,c 的值似乎没有增加。
c 始终为 0

for(String s : scoreVector)
    {
        comboVector.add(userVector.get(c) + ": " + s);
    }

from the code it seems that the value of c is not incrementing..
c is always 0

for(String s : scoreVector)
    {
        comboVector.add(userVector.get(c) + ": " + s);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文