Here is the piece of code we worked on Tuesday.
Remember that the for loop leaves x
as the length of the String
(not found) while indexOf()
returns -1.
import java.util.Scanner;
public class Main {
static Scanner kb = new Scanner(System.in);
public static void main(String[] args) {
int x;
// 1
// 01234567890
String line="ab,cd,e1,23,45";
String f, l;
//for ( x = 0; x< line.length() && line.charAt(x) != ',' ; x++);
// search for comma
x = line.indexOf(",");
while ( x != -1 ) {
// get chunk
f = line.substring(0,x);
System.out.println(f);
//take remainder of string and repeat
line = line.substring(x+1);
// search for comma
x = line.indexOf(",");
}
System.out.println(line);
//System.out.println(x);
/*
if ( x < line.length()) {
f = line.substring(0,x);
l = line.substring(x+1);
System.out.println(f);
System.out.println(l);
} else {
System.out.println("There is no comma...");
}
*/
}
}