普通的表达

时间:2011-03-09 05:33:04

标签: java

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(" ")))

1 个答案:

答案 0 :(得分:1)

因为字符串的长度可以是0,在这种情况下,您将得到一个ArrayIndexOutOfBounds异常。