I have a program i have to build for class using the Player class. i need to be able to remove add show and save and i cant seem to get the for loop to work in the methods. and when i tried passing the class player to the methods it didnt work either.

help me thnks :-)

package project1;

import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;

public class Main {

    public static void main(String[] args) throws Exception{
        Scanner input = new Scanner(System.in);
        String add = "ADD";
        String remove = "REMOVE";
        String show = "SHOW";
        String save = "SAVE";
        String quit = "QUIT";

        ArrayList<Player> playerArrayList = new ArrayList<Player>();
        boolean run = true;
        while (run == true) {
            System.out.println("What operation next? " +
                    "(ADD, REMOVE, SHOW, SAVE, QUIT)?");
            String operation = input.nextLine();

            if (operation.equalsIgnoreCase(quit)) {
                run = false;
                System.out.println("** GOODBYE! **");

            } else if (operation.equalsIgnoreCase(add)) {
                AddPlayer(input, playerArrayList);

            }//end add if statement
            else if (operation.equalsIgnoreCase(remove)) {
                RemovePlayer (input, playerArrayList, player);

            } else if (operation.equalsIgnoreCase(save)) {
                SavePlayer(input, playerArrayList);

            }else if (operation.equalsIgnoreCase(show)) {
                ShowPlayer(input, playerArrayList);
            }

        }//End of While
        }//end main

    public static void AddPlayer(Scanner input, ArrayList playerArrayList) {

        System.out.println("Please enter player's Last name: ");

        String lastName = input.nextLine();

        System.out.println("Please enter player's First name: ");

        String firstName = input.nextLine();

        System.out.println("Please enter number of points scored for " +
                firstName + " " + lastName + ": ");

        int points = input.nextInt();

        System.out.println("Please enter number of assists for " +
                firstName + " " + lastName + ": ");

        int assists = input.nextInt();

        System.out.println("Please enter penalty kick percentage (" +
                "example 0.25 for 25%) for " + firstName + " " + lastName + ":");

        double kickPercent = input.nextDouble();

        //set player class with new stats
        Player player = new Player();
        player.setFirstName(firstName);
        player.setLastName(lastName);
        player.setPointsScored(points);
        player.setAssists(assists);
        player.setPenaltyKickRate(kickPercent);

        playerArrayList.add(player);

        //show player created
        player.show();

    }//End AddPlayer Method

    public static void RemovePlayer(Scanner input, ArrayList playerArrayList) {

        System.out.println("Please enter last name of player to remove:");
        String lastName = input.nextLine();
        System.out.println("Please enter first name of player to remove:");
        String firstName = input.nextLine();

        for (Player player : playerArrayList) {
            if (player.getLastName().equalsIgnoreCase(lastName) &&
                    player.getFirstName().equalsIgnoreCase(firstName)) {

                System.out.println(player);
            }
        }
    }

    public static void ShowPlayer(Scanner input, ArrayList playerArrayList) {

        System.out.println("Please enter last name of player to remove:");
        String lastName = input.nextLine();
        System.out.println("Please enter first name of player to remove:");
        String firstName = input.nextLine();

        for (Player player : playerArrayList) {
            if (player.getLastName().equalsIgnoreCase(lastName) &&
                    player.getFirstName().equalsIgnoreCase(firstName))

                player.show();
        }
    }

    public static void SavePlayer (Scanner input, ArrayList playerArrayList) throws Exception {

        System.out.println("Please enter file name:");
        String nameOfFile = input.nextLine();

        FileWriter playerTxt = new FileWriter(nameOfFile);
        PrintWriter newPlayerTxt = new PrintWriter(playerTxt);

        for (Player player : playerArrayList) {
            newPlayerTxt.println(player.Show());
        }//End For



    }
}

The errors are in the For loops in the remove, save, and show methods

VernonDozier commented: Code tags on first post. +27

Recommended Answers

All 2 Replies

You should specify a type for the ArrayList in functions like this:

public static void SavePlayer (Scanner input, ArrayList playerArrayList[B])[/B]

Change to:

public static void SavePlayer (Scanner input, ArrayList <Player> playerArrayList)

Thanks so much! It made me mad for so long lol.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.