简体   繁体   English

检查数组时出现ArrayIndexOutOfBoundsException?

[英]ArrayIndexOutOfBoundsException when checking an array?

I've been making a instant chat program, and wanted to make it possible for users to "whisper" or private message one another. 我一直在做一个即时聊天程序,并希望使用户能够“窃窃私语”或私人消息。 The way I implemented that is the user would type: 我实现的方式是用户输入:

/w [username] [message] / w [用户名] [消息]

I then send it to the server which sends it to all the users. 然后我将它发送到服务器,然后将其发送给所有用户。 The users then have to check to see if its sent to them, this is that method: 然后用户必须检查它是否发送给他们,这是该方法:

if (message.startsWith("/msg/")) {
    message = message.trim();
    message = message.substring(5);
    String[] words = message.split("//s");
    String UserName = null;
    try {
    String username = words[2];
    UserName = username;
    System.out.println(UserName);
    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println("Error reading the whisper command!");
    }
    if (UserName == client.getName()) {
        List<String> list = new ArrayList<String>(Arrays.asList(words));
        list.removeAll(Arrays.asList(2));
        words = list.toArray(words);
        String text = words.toString();
        text = "You've been whispered! " + message;
        console(text);
    }

Everytime I send a /w when I'm testing it always give the ArrayIndexOutOfBoundsException. 每当我在测试时发送一个/ w,它总是给出ArrayIndexOutOfBoundsException。 I also modify the message in the sending. 我也在发送中修改消息。 Heres that method: 继承人的方法:

else if (message.toLowerCase().startsWith("/w")) {
    message = "/msg/" + client.getName() + message;
    message = "/m/" + message + "/e/";
    txtMessage.setText("");
}

I also added a whole bunch more options for the actual code for the users, I made it /whisper, /m, /msg, and /message, but those are all just copies of this with a different input. 我还为用户的实际代码添加了很多其他选项,将其设置为/ whisper,/ m,/ msg和/ message,但是这些都是带有不同输入的副本。 Why is it giving me an ArrayIndextOutOfBoundsException, when the 3rd place in the words array SHOULD be the username that the sender is trying to send it to. 当单词array中的第三位应该是发送方试图将其发送到的用户名时,为什么会给我ArrayIndextOutOfBoundsException。 Obviously this probably isn't the best way to send private messages, and if any of you guys have a simpler way I can implement to my server, please go ahead and let me know! 显然,这可能不是发送私人消息的最佳方法,如果你们中的任何人都可以将其实施到服务器上,使用简单的方法,请继续告知我! Just know that I am a young, new programmer and so I will probably have a lot of questions. 只知道我是一个年轻的新程序员,所以我可能会有很多问题。

The slashes in your split() regex are backwards. split()正则表达式中的斜杠是向后的。 You need 你需要

String[] words = message.split("\\s");

You can also just use a space 您也可以只使用一个空格

String[] words = message.split(" ");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 列印阵列背面时出现ArrayIndexOutOfBoundsException - ArrayIndexOutOfBoundsException when printing the reverse of an array 遍历数组时出现ArrayIndexOutOfBoundsException - ArrayIndexOutOfBoundsException when iterating through an array 尝试反转数组时出现ArrayIndexOutOfBoundsException - ArrayIndexOutOfBoundsException when trying to reverse an array 初始化对象的二维数组时,ArrayIndexOutOfBoundsException - ArrayIndexOutOfBoundsException when initializing a 2dimensional array of objectts 迭代遍历数组的所有元素时出现ArrayIndexOutOfBoundsException - ArrayIndexOutOfBoundsException when iterating through all the elements of an array ArrayIndexOutOfBoundsException 在数组中查找具有特定字母的单词时 - ArrayIndexOutOfBoundsException when finding words in an array that and with a specific letter 设置数组的值时,For循环导致ArrayIndexOutOfBoundsException - For loop causing ArrayIndexOutOfBoundsException when setting value of array Android:解析JSON Array时出现ArrayIndexOutOfBoundsException - Android: ArrayIndexOutOfBoundsException when parsing JSON Array 检查大小后的ArrayIndexOutOfBoundsException - ArrayIndexOutOfBoundsException after checking size 初始化数组对象时java.lang.ArrayIndexOutOfBoundsException - java.lang.ArrayIndexOutOfBoundsException when initializing array objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM