I am having issues with a class project, one part of it specifically.
this is what the assignment says Write a program that manages a list of up to 10 players and their high scores in the computer's memory - do not write to the disk. Use one array of Player objects. The Player class should store the players name and score, with suitable constructors, accessors, and mutators. The Player class should also override the equals and toString methods appropriately. Your high score program should support the following features: - add a new player and score - print all of the players' names and their scores to the screen - allow the user to enter a player's name and output that player's score or a message if that player's name has not been entered - allow the user to enter a player's name and remove the player from the list
Create a menu system that allows the user to select which option to invoke.
So far I have been able to create a menu, add a player and a score, and print all of the players added. The issue I am having is searching the array for a specific name and then being able to remove that player.
below is a snippet from my handler class that handles the information.
public void removePlayer(String playerName)
{
//remove player from player array
//it doesn't work and I do not know how to get it to work
//the issue I have is I dont know how to compare a string to
//an array of players it doesnt compile correctly
//with the error "Incompatible Operand types players and String".
for (int i = 0; i< players.length; i++)
{
if(this.players[i] == playerName)
{
for (int x = i; x<players.length; x++)
{
this.players[x] = this.players[x+1];
}
}
}
}
I understand this is wrong but I do not know what to do to make it correct. here is the players class that takes in the info.
public class players {
private String playerName;
private int playerScore;
public players(){
}
public players(String newName, int newScore){
playerName = newName;
playerScore = newScore;
}
@Override
public boolean equals(Object otherPlayer){
return false;
}
@Override
public String toString(){
return playerName + " " + playerScore;
}
}
and lastly the handler code for the array. and how it adds players.
public class highScoreHandler {
private final static int PLAYER_LIMIT=10;
private players[] players;
private int pointer;
public highScoreHandler(){
this.players = new players[PLAYER_LIMIT];
for (int i = 0; i< players.length; i++)
{
this.players[i] = new players();
}
this.pointer = 0;
}
public boolean addPlayer(players newPlayer)
{
//add new player to player array
if(this.pointer > this.players.length-1)
{
System.out.println("please remove a player.");
return false;
}
else
{
this.players[this.pointer] = newPlayer;
this.pointer +=1;
System.out.println("Player has been added!");
return true;
}
}
any help would be greatly appreciated, thank you.