Re: Error with my filter program
SamuelXiao wrote:
My program is doing filtering.
when I type input.txt, the compiler complains:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: -1
at java.lang.String.substring(String.java:1938)
at FilterProgram.main(FilterProgram.java:35)
Java Result: 1
How can I fix it and can anyone tell the problem?
SamuelXiao,
Thank you for posting a compilable example.
Here is a simplified version.
public class Scratch {
public static void main(String[] args) {
String tryread =
"public class Question1{ \t\t/*The parts";
int doubleslash = tryread.indexOf("//");
int block1 = tryread.indexOf("/*");
int block2 = -1;
if ((doubleslash >= 0) || block1 >= 0) {
if (doubleslash < block1) {
tryread = tryread.substring(0, doubleslash);
System.out.println("doubleslash");
}
}
}
}
at the line: "if (doubleslash < block1) {"
the debugger shows values for:
tryread = "public class Question1{ \t\t/*The parts"
doubleslash = -1
block1 = 30
When I run this program I get:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: -1
at java.lang.String.substring(Unknown Source)
at Scratch.main(Scratch.java:11)