ArrayIndexOutOfBoundsException与每个循环有关

问题描述 投票:0回答:1

我是初学者,正在构建经典的Nim游戏。到目前为止,我已经完成了添加和删除播放器的功能。现在,我想显示播放器,但碰到了ArrayIndexOutOfBoundsException。我已经检查了这篇文章,但是找不到我的代码有什么问题。 For-Each Loop Java Error ArrayIndexOutOfBoundsException

[基本上,我使用NimPlayer中的吸气剂来获取信息,并且使用每个循环来打印出玩家。我已经检查了循环的长度,但是没有用。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 at Nimsys.processCommands(Nimsys.java:35) at Nimsys.main(Nimsys.java:12)

  1. 第12行指的是processCommands
  2. 第35行是displayPlayer()中的processCommands功能。

非常感谢您的帮助,感谢您的友好和耐心。

这里是我的Nimsys的一部分:

public class Nimsys {

private NimModel nimModel;

public static void main(String[] args) {
    Nimsys nimsys = new Nimsys();
    nimsys.processCommands();
}


Scanner in = new Scanner(System.in);

private void processCommands() {
    this.nimModel = new NimModel();
    System.out.println("Welcome to Nim\n");
    while (true) {
        System.out.print('$');
        String [] commandin = in.nextLine().split(" ");

        if (commandin[0].equalsIgnoreCase("displayplayer")) {
            displayPlayer(commandin[1]);
    }
}
private void displayPlayer(String userName) {
    if (!userName.isEmpty()) {
        nimModel.displayCertainPlayer(userName);
        return;
    } else {
        nimModel.displayAllPlayers();
        return;
    }
}

我的NimModel的一部分:

public void displayCertainPlayer(String inputUser) {
    if (inputUser != null && playerList.size() != 0 ) {
        for (NimPlayer player : playerList) {
            String userCheck = player.getUserName();
            String userName = player.getUserName();
            String familyName = player.getFamilyName();
            String givenName= player.getGivenName();
            int gamesWon = player.getGamesWon();
            int gamesPlayed = player.getGamesPlayed();

            if (userCheck.equals(userName)) {
                System.out.println(userName + "," + familyName + "," + givenName + "," + gamesPlayed + " games," + gamesWon + " wins");
            } else {
                System.out.println("The player does not exist");
            }          
        }
    } 
}

public void displayAllPlayers() {
    if (playerList.size() != 0) {
        for (NimPlayer player : playerList) {
            String userName = player.getUserName();
            String familyName = player.getFamilyName();
            String givenName= player.getGivenName();
            int gamesWon = player.getGamesWon();
            int gamesPlayed = player.getGamesPlayed();

            System.out.println(userName + "," + familyName + "," + givenName + "," + gamesPlayed + " games," + gamesWon + " wins");
        }
    } else {
        System.out.println("There is no players.");
    }

}

NimPlayer的一部分:

ublic class NimPlayer {


private final String userName;
private String familyName;
private String givenName;

private int gamesPlayed;
private int gamesWon;
private int winRatio;



public NimPlayer(String userName, String familyName, String givenName) {

    this.userName = userName;
    this.familyName = familyName;
    this.givenName = givenName;
    this.gamesPlayed = 0;
    this.gamesWon = 0;
}
// getters and setters
}
java arraylist indexoutofboundsexception
1个回答
1
投票

问题出在displayPlayer(commandin[1])行中。索引1没有任何内容,因为我假设(在阅读了您的实现之后),仅输入“ displayplayer”应该显示完整的玩家列表。拆分此字符串时,它返回的只是该字符串的数组,而不是空字符串"",就像将其传递给方法时所期望的那样。

我建议像这样更改您的代码:

String [] commandin = in.nextLine().split(" ");
if(commandin[0].equalsIgnoreCase("displayplayer")){ // a specific player to show
  if (commandin.length > 1) {
    displayPlayer(commandin[1]);
  } else {
    displayPlayer(""); // All Players
  }
}

您还可以创建一个名为displayPlayers()的新方法,当commandin[1]为空时可以调用该方法

© www.soinside.com 2019 - 2024. All rights reserved.