Java – returns only the last element in the ArrayList

I've been teaching myself Java. I've always insisted on a problem. No matter what I do, it seems that I can't solve it I did some research, but all the options offered seemed useless I hope you can teach me something

I have one Txt file containing:

AccountName1:Password1  
AccountName2:Password2  
AccountName3:Password3  
AccountName4:Password4  
AccountName5:Password5

Then read the elements of the file and insert them into the list:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public abstract class AccountFileReader {

  private static Scanner sc;

  public static void main(String[] args) {  

      try {         
        // Enables ability to find file in any OS.
           String file = File.separator + "some folder name"
                         + File.seperator + "AccNamePw.txt";

           File f = new File(file);
           sc = new Scanner(f);

           List<AccountInfo> accounts = new ArrayList<AccountInfo>();
           String name = "";
           String password = "";

           while (sc.hasNext()){
            // Reads and checks if there is a new line
               String line = sc.nextLine();
            // Creates delimiter to make the different elements on file f
               String[] details = line.split(":");
            // Initializes 1st element
               name = details[0];
            // Initializes 2nd element
               password = details[1];           
            // Creates new object "a" that has the 2 elements from each line
               AccountInfo a = new AccountInfo(name,password);
            // Adds the "a" object to the "accounts" List
               accounts.add(a);
           }
        // Iterates list and prints out the list
           for(AccountInfo a: accounts){
        // The hiccup is in here somewhere. This for loop isn't working in 
        // a way I think it's supposed to.

        // Create new object of the getter,setter class to use in this loop
           AccountInfo namPw = new AccountInfo(name,password);
              name = namPw.getName();
              password = namPw.getpassword();               
              System.out.println(a.toString() + "         " + name 
                                 + " " + password);
           }
        } catch (FileNotFoundException e) {e.printStackTrace();}
    }
}

Getter / setter classes are as follows:

public class AccountInfo{
private String name;
private String password;

public AccountInfo(String name,String password) {
    this.setName(name);
    this.setPassword(password);
}

public void setName(String name) { this.name = name; }

public String getName() { return name; }

public void setPassword(String password) { this.password = password; }    

public String getpassword() { return password; }

public String toString(){ return name + " "+ password; }
}

My output is:

AccountName1:Password1      AccountName5:Password5  
AccountName2:Password2      AccountName5:Password5  
AccountName3:Password3      AccountName5:Password5  
AccountName4:Password4      AccountName5:Password5  
AccountName5:Password5      AccountName5:Password5

But I want it back:

AccountName1:Password1      AccountName1:Password1  
AccountName2:Password2      AccountName2:Password2  
AccountName3:Password3      AccountName3:Password3  
AccountName4:Password4      AccountName4:Password4  
AccountName5:Password5      AccountName5:Password5

I know a.tostring() returned correctly, but my nampw Getname() and nampw GetPassword () only gives the last element of the list

What do I not understand or lack? How to make nampw Getname() and nampw Getpassword() returns list correctly?

Solution

The problem is to declare the name and password before the while loop These variables store the last user name and password encountered When the while loop ends, these variables store the values of accountname5 and password5, respectively

When you enter the second for loop, first create a new useraccount with the name and password that stores accountname5 and password5

If you only want to print this list, you do not need to create a copy of the contents of the list Just do it:

for(AccountInfo a : accounts) {
     System.out.println(a.toString() + " " + a.getName() + " " + a.getpassword());
 }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>