public void thisisnotworking()
{
Scanner input=new Scanner(System.in);
String cardA;
String cardB;
System.out.print("Enter Card: "); cardA=input.nextLine();
System.out.print("Enter Card "); cardB=input.nextLine();
Card a = game.get(game.indexOf(cardA));
Card b = game.get(game.indexOf(cardB));
This code is part of a method i’ve written. Basically the user inputs the card that is one of the cards in an array list and then another card. Currently I’m getting an IndexOutOfBoundsException error when I’m running this part of the code.
Can anyone tell me what is wrong with it?
It’s hard to say without seeing
game, but seems likecardAandcardBare not ingame, sogame.indexOf(cardA)returns -1 andgame.get(-1)throws the exception.Also, looking at your code, you search for
Stringingame, and then takes an element fromgame, which is aStringand assigns it to aCardobject. You cannot cast from String to any other class thanObject, sinceStringis a final class.