Why do I keep getting only tha last object value i

2020-02-16 04:58发布

I have managed to put all objects into arraylist but I am not able to print all values. Only the last one is getting printed, regardless of method used.

It is not getting printed through ArrayList only, which makes me wonder if the objects pushed are the same. 

If it is, how do I change that? I have attached the program (run FileInput.java):

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

import javax.swing.text.html.HTMLDocument.Iterator;

//lecture notes(L1) method
public class FileInput {
    static String first;
    static String second;
    static String third;
    static String fourth;
    static String fifth;
    static String sixth;

    static int num = 1;
        public static void main(String[] args)throws FileNotFoundException, IOException{
        Scanner input = new Scanner(new File("player.txt"));
        String data = null;

        PrintWriter output = new PrintWriter("outfile.txt");

        // Player1 user = new Player1();

        ArrayList<Player1>listOfPlayers = new ArrayList<>();

        Player1 user = new Player1();

        // Tokenizing
        System.out.println("CSCI213 Players Management System");
        while(input.hasNextLine()){

            System.out.println("\nPlayer " + num);
            data = input.nextLine();
            StringTokenizer token = new StringTokenizer(data,"|");

            // int t = token.countTokens();
            // System.out.println("t is:" + t);

            first = token.nextToken().trim();
            user.setLoginname(first);

            second = new String(token.nextToken("|"));
            user.setPassword(second);

            third = new String(token.nextToken("|"));
            user.setChips(third);

            fourth = new String(token.nextToken("|"));
            user.setUsername(fourth);

            fifth = new String(token.nextToken("|"));
            user.setEmail(fifth);

            sixth = new String(token.nextToken("|"));
            user.setBirthdate(sixth);

            // user.display();

            listOfPlayers.add(user);
            System.out.println("Size is: " + listOfPlayers.size());
            // System.out.println(user.loginname);
            // System.out.println(listOfPlayers.get(num-1).loginname);
            num++;
            // output.write(data);
            // output.write("\r\n");
        }
        int x = listOfPlayers.size();
        System.out.println("Size is: " + x);
        System.out.println(listOfPlayers);

        // Display address of required
        Player1 p = new Player1();

        for(int i = 0; i < x; i++){
            p = listOfPlayers.get(i);
            System.out.println(p.loginname);
        }

        for(Player1 e:listOfPlayers){
            System.out.println(e.loginname);
            System.out.println(e.email);
        }

        while(input.hasNextLine()){
            data = input.nextLine();
            output.write(data);
            output.write("\r\n"); 
        }

        input.close();
        output.close();
    }
}

// Store all player information
public class Player1 {
        static String loginname;
        static String password;
        static String chips;
        static String username;
        static String email;
        static String birthdate;

        /*
        public Player1(String loginname, String password, 
                String username, String email, String birthdate){
            this.loginname = loginname;
            this.password = password;
            this.username = username;
            this.email = email;
            this.birthdate = birthdate;
        }
        */

        public Player1() {
            // TODO Auto-generated constructor stub
        }

        public static String getLoginname() {
            System.out.print("loginname: ");
            return loginname;
        }

        public static void setLoginname(String loginname) {
            Player1.loginname = loginname;
        }

        public static String getPassword() {
            System.out.print("password: ");
            return password;
        }

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

        public static String getUsername() {
            System.out.print("username: ");
            return username;
        }

        public static void setUsername(String username) {
            Player1.username = username;
        }

        public static String getEmail() {
            System.out.print("email: ");
            return email;
        }

        public static void setEmail(String email) {
            Player1.email = email;
        }

        public static String getBirthdate() {
            System.out.print("birthdate: ");
            return birthdate;
        }

        public static void setBirthdate(String birthdate) {
            Player1.birthdate = birthdate;
        }

        public static String getChips() {
            System.out.print("chips: ");
            return chips;
        }

        public static void setChips(String chips) {
            Player1.chips = chips;
        }

        public void display() {
            System.out.println("Name: " + this.username);
            System.out.println("Email: " + this.email);
            System.out.println("Birthdate: " + this.birthdate);
            System.out.println("Login ID: " + this.loginname);
            System.out.println("Balance Chips: " + this.chips);
        }
/*
        @Override
        public String toString() {
            return "toString()=" + this.loginname + "\n";
        }       
*/      
}

2条回答
该账号已被封号
2楼-- · 2020-02-16 05:14

You have two errors :

  1. You are adding the same Player1 instance to the list over and over again. You should move Player1 user = new Player1(); into the loop that adds the players.

    Change

    Player1 user = new Player1();
    
    // Tokenizing
    System.out.println("CSCI213 Players Management System");
    while (input.hasNextLine()) {
    

    to

    // Tokenizing
    System.out.println("CSCI213 Players Management System");
    while (input.hasNextLine()) {
        Player1 user = new Player1();
    
  2. The members of the Player1 class are all static, so even if you fix the first issue, all instances of Player1 will share these members. You should change them to non static.

    Change

    public class Player1 {
        static String loginname;
        static String password;
        static String chips;
        static String username;
        static String email;
        static String birthdate;
    

    to

    public class Player1 {
        String loginname;
        String password;
        String chips;
        String username;
        String email;
        String birthdate;
    
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-02-16 05:30

Simple:

 Player1 user = new Player1();

You are adding the same object again and again. Put that statement into your loop instead. You want to add a completely new Playwer object during each loop iteration!

But even then, things wouldn't work out; because (as Eran figured): your Player class has only static fields. That is like "cheating"; because it means that all Player objects would see the same fields, too (because static fields are shared between all instances of a class!)

In other words: static is an abnormality in good OO design. You don't use it as default; to the contrary: you only make fields static in special corner cases (see here for some examples).

查看更多
登录 后发表回答