-1 java arrays error-handling exception
我正在做一项任务,我必须做几个简单的字符串操作.我想我有最后一个想通了,和它的作品本身,但是当我与其他字符串操作把它放在一起,给我一个arrayoutofbounds异常错误这是行不通的.有什么建议?这是我制作的小代码
public static void main (Strings[] args) {
Scanner sc = new Scanner(System.in);
String theSentence = sc.nextLine();
String [] theWords = theSentence.split(" ");
Arrays.sort(theWords);
System.out.println(theWords[1]);
System.out.println(Arrays.toString(theWords));
}
Run Code Online (Sandbox Code Playgroud)
当它与其余代码放在一起时,这不起作用,即使它本身是有效的.作为参考,这段代码应该用一个小句子,并按字典顺序给我一个最小的单词.例如:输入:"4 WHAT WAIT IS THIS"输出将是"IS"
代码假定它theWords总是至少有两个元素.如果用户提供的句子没有任何空格,theWords则永远不会在位置1中获取元素,因此程序将崩溃System.out.println(theWords[1]);
| 归档时间: |
|
| 查看次数: |
83 次 |
| 最近记录: |