Reputation: 252
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());
why expression ....
while ((!line.substring(j, j + 1).equals(" ")) && (j < line.length()))
not equal with
while ((j < line.length())&&(!line.substring(j, j + 1).equals(" ")))
Upvotes: 0
Views: 119
Reputation: 1243
Because the string could be of length 0 in which case you will get an ArrayIndexOutOfBounds exception.
Upvotes: 1