简体   繁体   中英

ArrayList for loop printing out indexes instead of value

I've created a method to read a text file and pull out the name of a contact from each line.

private  ArrayList<String> readContacts()
    {
        File cFile = new File ("Contacts.txt");
        BufferedReader buffer = null;
        ArrayList <String> contact = new ArrayList<String>();
        try
        {
            buffer = new BufferedReader (new FileReader (cFile));
            String text;
            String sep;
            while ((sep = buffer.readLine()) != null)
            {       
                String [] name = sep.split (",");
                text = name[1];
                contact.add(text);  
            }   
        }
        catch (FileNotFoundException e)
        {

        }
        catch (IOException k)
        {

        }
        return contact;
    }

I'm trying to populate a JList with each contacts name using the method I've created above using this:

model = new DefaultListModel();
for (int i = 1; i < readContacts().size(); i++)
{
  ArrayList <String> name = readContacts();
  model.addElement(name);
}

nameList = new JList (model);
add(nameList);

When I run the program, the JList only has the numbers 1-10, instead of each of the contacts names. Is the problem I'm running into here logical or problems with syntax? Any help would be great, thanks!

  1. Don't call readContacts() from within the for loop as that makes no sense. You're creating a new ArrayList multiple times and then adding the entire same ArrayList to your JList, in other words, each element in your JList is an ArrayList (???).
  2. Instead call it in the for loop condition or before the for loop.
  3. Do not have empty catch(...) blocks. Doing this is the programming equivalent of driving your car with your eyes closed -- very dangerous.

For example,

model = new DefaultListModel();
// call readContacts() only *once*
for (String name: readContacts()) {
    model.addElement(name);
}

I would say we may create a ArrayList object first, then assign the new readContacts() to this object. After that, read the elements in a loop, if you want, you can print out each elements in the ArrayList to ensure those are you want.

If your program is running, you do not have a syntax problem. Your program is running, but is not giving the expected results. That is a logical problem.

You state:

"the JList only has the numbers 1-10, instead of each of the contacts names."

When I look at your readContacts() , you populate your contact List with name[1]. What is in name[1]? I'm lead to believe that name[1] contains 1 - 10 as you read through your file. What is in name[0], or name[2] or name[3], if they even exist. So my first suggestion will be to try a different index for name since name[1] isn't giving you the right result.

Second, you populated your model with the ArrayList name, instead of using the contents of name. Follow Hovercraft Full Of Eels example to populate your model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM