IndexOutOfBoundsException error

后端 未结 4 915
栀梦
栀梦 2021-01-29 10:53
public void thisisnotworking()
{
    Scanner input=new Scanner(System.in);
    String cardA;
    String cardB;
    System.out.print(\"Enter Card: \"); cardA=input.nextLi         


        
4条回答
  •  無奈伤痛
    2021-01-29 11:25

    It's hard to say without seeing game, but seems like cardA and cardB are not in game, so game.indexOf(cardA) returns -1 and game.get(-1) throws the exception.

    Also, looking at your code, you search for String in game, and then takes an element from game, which is a String and assigns it to a Card object. You cannot cast from String to any other class than Object, since String is a final class.

提交回复
热议问题