import java.util.LinkedList;
public class GamesChallenge {
private static LinkedList<Game> gameList = new LinkedList<>();
public static void main(String[] args){
//adding the games to the LinkedList
addGame("Call of Duty", 300);
addGame("Last day on Earth", 500);
addGame("Fall Guys", 3000);
System.out.println(gameList);
//try to find the game in the LinkedList
findGame("Fall Guys");
//try to remove a game
removeGame("Fall Guys");
System.out.println(gameList);
}
public static void addGame(String name, int memory){
// check if the song is in the linkedList
Game game = new Game(name, memory);
while(findGame(name) == false){
//if the game does not exist then the game is added
gameList.add(game);
break;
}
}
public static boolean findGame(String name){
// check if the game is in the LinkedList
if(gameList.indexOf(gameList.contains(name)) >= 0){
//if the index is bigger or equal to 0 (the game is in the LinkedList) then it will return true
return true;
}
//else it will return false
else return false;
}
//here is where I have the problem
public static void removeGame(String name){
gameList.remove(gameList.indexOf(gameList.contains(name)));
}
}
class Game {
private String name;
private int memory;
public Game(String name, int memory){
this.name = name;
this.memory = memory;
}
@Override
public String toString(){
return name + ":" + memory;
}
public String getName() {
return name;
}
public int getMemory() {
return memory;
}
}
I am new to programming and i am trying to learn. I tried to remove the game from the LinkedList but an error is thrown, the index is out of range and I do not understand why.