lundi 17 avril 2017

Why do I keep getting only tha last object value in Java ArrayList?

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";
        }       
*/      
}

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). How do I make all the objects get displayed?






Comments
0 Comments

0 commentaires:

Enregistrer un commentaire