Create a program that allows you to create a fantasy football roster based on the existing list of available players. Your team can only have 5 players, so create an array that can store 5 possible players. Prompt the user to pick 5 players to add to their team. If the player is available in the list of availablePlayers, then add that player to the users array, and remove that player from the availablePlayers list. Then prompt the user to pick another player. Once all five players have been added, then print the list in the console. Create a search method to find the index at which the player is located in the availablePlayers list so you can remove the player, and make sure that they are an eligible pick.

Respuesta :

Answer:

  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. public class Main {
  4.    public static void main(String[] args)
  5.    {
  6.        String availablePlayer [] = {"Mike", "Jordan", "Alvin", "Calvin", "Rocky", "John", "Harris", "Faris", "Mark"};
  7.        String userTeam [] = new String[5];
  8.        Scanner input = new Scanner(System.in);
  9.        for(int i=0; i < 5; i++){
  10.            System.out.print("Pick a player: ");
  11.            String target = input.nextLine();
  12.            int found = search(availablePlayer, target);
  13.            if(found != -1){
  14.                userTeam[i] = availablePlayer[found];
  15.                String newArray [] = new String[availablePlayer.length-1];
  16.                for(int j = 0; j < found; j++)
  17.                {
  18.                    newArray[j] = availablePlayer[j];
  19.                }
  20.                for(int k = found; k < newArray.length; k++){
  21.                    newArray[k] = availablePlayer[k + 1];
  22.                }
  23.                availablePlayer = newArray;
  24.            }
  25.            else{
  26.                System.out.println("Player not found.");
  27.            }
  28.        }
  29.        System.out.println(Arrays.toString(userTeam));
  30.        System.out.println(Arrays.toString(availablePlayer));
  31.    }
  32.    public static int search(String playerList[], String target){
  33.        for(int i=0; i < playerList.length; i++){
  34.            if(playerList[i].equals(target)){
  35.                return i;
  36.            }
  37.        }
  38.        return -1;
  39.    }
  40. }

Explanation:

The solution code is written in Java.

Firstly, create an array of available players and another array for user team (Line 7 - 8).

Next user Scanner object to get target player from user (Line 12 - 14). To get a target player, a search method has been written (Line 40 - 47) which will return the index where the target is found from the array.

When the index of the target is identified from the available player array, that index will be used to extract the target player from the availablePlayer array and assign it to the current element of the userTeam array (Line 17 - 18).

Since Java doesn't offer in-built method to remove the element from an existing array, the code from 19 -28 are written to handle this removal.

At the end, display the userTeam array and availablePlayer array to console (Line 36 - 37).