【问题标题】:Array Out of Bounds issue数组越界问题
【发布时间】:2012-11-04 16:20:29
【问题描述】:

我有一个非常简单的程序,只有一个文本框和一个按钮。

用户被告知在框中输入两种颜色的名称,并用空格分隔。

例如“红绿” 输出将在屏幕上打印,“苹果是红色的,带有绿点。”。

但是,当屏幕上只输入一个单词时,我需要它来工作。我正在使用一个包含拆分字符串的数组。当我只输入红色时,我会收到此错误。

“AWT-EventQueue-0”java.lang.ArrayIndexOutOfBoundsException:

代码如下:

String userInput = textField.getText();
String[] userInputSplit = userInput.split(" ");
String wordOne = userInputSplit[0];
String wordTwo = userInputSplit[1];
if (wordTwo !=null){
                System.out.println("The apple is " + wordOne + " with " + wordTwo + " dots.");
                } else {
                 System.out.println("The apple is " + wordOne + " with no colored dots.");
                }

【问题讨论】:

    标签: java arrays string split indexoutofboundsexception


    【解决方案1】:

    可以像这样简单地做一些事情:

    String wordOne = userInputSplit[0];
    String wordTwo = null;
    if (userInputSplit.length > 1){
        //This line will throw an error if you try to access something 
        //outside the bounds of the array
        wordTwo = userInputSplit[1];  
    }
    if (wordTwo !=null) {
        System.out.println("The apple is " + wordOne + " with " + wordTwo + " dots.");
    } 
    else {
        System.out.println("The apple is " + wordOne + " with no colored dots.");
    }
    

    【讨论】:

    • 我会使用String wordTwo = userInputSplit.length > 1 ? userInputSplit[1] : null;,我发现它更容易解析,但我就是这样。
    • @DanielFischer 是的,那会更干净,但由于这个问题非常基本,我认为使用条件运算符只会给 OP 增加更多混乱。
    • @MadProgrammer 有什么问题?我看不出有什么问题,但目前我面前没有编译器。
    • 这很好用。如果我想添加第三种颜色,是否将“if (userInputSplit.length > 1)”更改为 > 2?
    • @DanielFischer 现在有,更新前没有。 if (userInputSplit.length > 1) 语句中的声明 String wordTwo = userInputSplit[1] 不会编译,已更新以纠正它;)
    【解决方案2】:

    你可以用 if 声明来包含你的打印逻辑:

    if (userInputSplit.length > 1) {
      String wordOne = userInputSplit[0];
      String wordTwo = userInputSplit[1];
      ...
    }
    

    【讨论】:

    • 非常感谢您的回复。这与 KDiTraglia 的响应一起工作。
    【解决方案3】:

    你也可以做一个前置条件检查,看看用户的输入是否包含空格...

    String userInput = textField.getText();
    userInput = userInput.trim(); // Remove any leading/trailing spaces
    if (userInput != null && userInput.length() > 0) {
        StringBuilder msg = new StringBuilder(64);
        if (userInput.contains(" ")) {
            String[] userInputSplit = userInput.split(" ");
            String wordOne = userInputSplit[0];
            String wordTwo = userInputSplit[1];
            msg.append("The apple is ").append(wordOne);
            msg.append(" with ").append(wordTwo).append(" dots");
        } else {
            msg.append("The apple is ").append(userInput).append(" with no colored dots.");
        }
        System.out.println(msg);
    } else {
        // No input condition...
    }
    

    这只是看待问题的另一种方式......

    【讨论】:

    • 非常感谢,其实我从你的sn-p那里学到了一些东西,可以用这个来做类似的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 2018-06-21
    • 2012-10-28
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    相关资源
    最近更新 更多