> "); String line = kb.nextLine(); line = li-6ren">
gpt4 book ai didi

java - 普通表达

转载 作者:行者123 更新时间:2023-12-01 15:55:05 25 4
gpt4 key购买 nike

Scanner kb = new Scanner(System.in);

System.out.print(">> ");
String line = kb.nextLine();

line = line.trim();
int i = 0;

do {
int j = i;
while ((!line.substring(j, j + 1).equals(" ")) && (j < line.length())) {
//while ((j < line.length())&&(!line.substring(j, j + 1).equals(" "))) {
j++;
}
System.out.println("-> " + line.substring(i, j));
i = j;
while ((line.substring(i, i + 1).equals(" "))&&(i<line.length())) {
//while ((i < line.length()) && (line.substring(i, i + 1).equals(" "))) {

i++;
}
}while (i < line.length());

为什么表达....

while ((!line.substring(j, j + 1).equals(" ")) && (j < line.length())) 

不等于

while ((j < line.length())&&(!line.substring(j, j + 1).equals(" ")))

最佳答案

因为字符串的长度可能为 0,在这种情况下,您将收到 ArrayIndexOutOfBounds 异常。

关于java - 普通表达,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5241777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com