I've decided to write my own subString method, however when I test it within my test driver I receive an error with one of my lines in my SubString method. This is the error:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.charAt(Unknown Source)
at unisa.util.StringImproved.subString(StringImproved.java:618)
This is my code for my subString:
public String subString(int start, int end) {
//empty string to append the new string's characters to
String subString = "";
//creating a new string to store content to allow function
String s1 = new String(content);
//read each character in the string and append based on the index
for (int i=start; i<=end; i++){
subString += s1.charAt(i);
}
//convert string back to a char array
content = subString.toCharArray();
return subString;
}
The error is picking up the line subString += s1.charAt(i); Anyone able to shed some light on what i'm doing wrong??
PS. this isn't for an assignment just a bit of boredom and wanting to re-create the java functions myself.
Thanks in advance!!
I have added my testing methods as per request!!
private int testSubString(String testID) {
int errorCount=0;
header(testID);
{
System.out.println("Test[getting substring from middle] ");
StringImproved str = new StringImproved("0123456789");
String result = str.subString(1,2);
errorCount+=handleResult(result,"1")?0:1;
}
{
System.out.println("Test[getting first characters] ");
StringImproved str = new StringImproved("0123456789");
String result = str.subString(0,2);
errorCount+=handleResult(result,"01")?0:1;
}
{
System.out.println("Test[getting last characters] ");
StringImproved str = new StringImproved("0123456789");
String result = str.subString(str.length()-3,str.length());
errorCount+=handleResult(result,"789")?0:1;
}
{
System.out.println("Test[out of range] ");
StringImproved str = new StringImproved("0123456789");
String result = str.subString(0,str.length()+1);
errorCount+=handleResult(result,null)?0:1;
}
{
System.out.println("Test[zero length source] ");
StringImproved str = new StringImproved("");
String result = str.subString(0,str.length()-1);
errorCount+=handleResult(result,null)?0:1;
}
return errorCount;
}
StringIndexOutOfBoundsExceptionon occassion. Also tell us with wich values your are testing the method.startis larger than the length of the input string orendis larger than the length of the input string orstartis larger thanend, orstartorendis negative? You have to at least think about what your method should do in these cases. You can leave it unspecified; but then, throwing aStringIndexOutOfBoundsExceptionis acceptable behaviour for the mentioned input values. I do not know with wich values you tested your method, so I have no clue to why throwing aStringIndexOutOfBoundsExceptionis wrong.startandendarguments and the length of the input string so that you get the expected results in the cases out of range and zero length source (you return an empty string in those cases, but according to the tests you are doing,nullis expected).