Home > Back-end >  Editing Array thats called from a method?
Editing Array thats called from a method?

Time:04-23

I'm really new to programming, so I'm sorry if this is really basic. I am creating a program in Java that creates a football player with various pieces of data, such as name, goals, games played, and assists. This then adds them to a football team class , i have created a method for adding, showing and removing players but i havent done much programming for a few weeks and im struggling to figure out the editing.

These are the methods.

    public void addPlayer() {
//creates a scanner to input the variables
        Scanner userInput = new Scanner(System.in);
        //creates a new player object
        Player newPlayer = new Player();
        // using the setters to enter the variables for the new player using the scanner
        System.out.println("Please type the name of the player:");
        newPlayer.setName(userInput.nextLine());

        System.out.println("Please type the age of the player:");
        newPlayer.setAge(userInput.nextInt());

        System.out.println("Please type the number of goals the player has scored:");
        newPlayer.setGoals(userInput.nextInt());

        System.out.println("Please type the number of assists the player has:");
        newPlayer.setAssists(userInput.nextInt());

        System.out.println("Please type the number of games the player has played:");
        newPlayer.setGamesPlayed(userInput.nextInt());

        System.out.println("Please type the position that the player primarily plays:");
        newPlayer.setPosition(userInput.next());

        //once the variables are entered, we add the player object to the arraylist
        playerList.add(newPlayer);
        System.out.println("Player Successfully Added:");

    }

public void showPlayers() {

        //for loop to iterate through the arraylist of players, outputting each variable
        for (int i = 0; i < playerList.size(); i  ) {
            System.out.println("Player "   i   "name: "   playerList.get(i).getName()   " Position: "   playerList.get(i).getPosition()   " age: "   playerList.get(i).getAge()   " number of goals: "   playerList.get(i).getGoals()
                      " number of assists: "   playerList.get(i).getAssists()   " games played: "   playerList.get(i).getGamesPlayed());
        }

    }

public void removePlayer() {
        for (int i = 0; i < playerList.size(); i  ) {
            System.out.println("Player "   i   "name: "   playerList.get(i).getName());
        }
        System.out.println("Enter the index of the player you want to remove");
        Scanner userInput = new Scanner(System.in);
        playerList.remove(userInput.nextInt());
        System.out.println("Player Successfully Removed:");
        for (int i = 0; i < playerList.size(); i  ) {
            System.out.println("Player "   i   "name: "   playerList.get(i).getName());

        }

(I have realised the previous method should call showPlayers instead of doing a whole new loop)

I'm now really struggling with the editPlayer method, I have made a start but my brain has gone blank, any help would be appreciated. I basically want to be able to call from the Show Players method and have the user input the index from the playerList or even their name and then be able to edit the data.

public void editPlayer() {
        this.showPlayers();
        Scanner userInput = new Scanner(System.in);
        System.out.print("Enter the index of the player you would like to edit: ");
        try {
            Integer name = userInput.nextInt();
            if (name = playerList.index()) {
            }
        }
    }

CodePudding user response:

Following is the implementation for updating one of the player from the list with given index.

public void editPlayer() {
    this.showPlayers();
    Scanner userInput = new Scanner(System.in);
    System.out.print("Enter the index of the player you would like to edit: ");
    try {
        Integer index = userInput.nextInt();
        Player playerToUpdate = null;
        // Filter out the player from the list, that has to be updated
        for(int i = 0; i < playerList.size(); i  ){
             if(i == index) {
                  playerToUpdate = playerList.get(i);
                  break;
             }
        }
        if(playerToUpdate != null){
           // Take input and update the data, you can ask for the data needs to be updated.
           // For eg Name
           System.out.println("Please type the name of the player:");
           playerToUpdate.setName(userInput.nextLine());
        }else{
           // The input index is not a valid index    
        }
    }
}
  • Related