【发布时间】: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