IndexOutOfBoundsException error

后端 未结 4 917
栀梦
栀梦 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:28

    Try this...

    public void thisshouldwork()
    {
        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();
        game.add(cardA);
        game.add(cardB);
        Card a = game.get(game.indexOf(cardA));
        Card b = game.get(game.indexOf(cardB));
    

    The problem is you aren't adding cardA or cardB to game so indexOf returns -1 and get throws an IndexOutOfBounds Exception

提交回复
热议问题