Question: JAVA: Exception in thread main java.lang.ArrayIndexOutOfBoundsException: -1 at MyString.indexOf(MyString.java:84) at MyStringTester,main(MyStringTester.java:47) I ran into this problem when testing the MyString class I created through a
JAVA: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at MyString.indexOf(MyString.java:84) at MyStringTester,main(MyStringTester.java:47)
I ran into this problem when testing the MyString class I created through a tester file. The following codes are the MyString.java and the MyStringTester.java (). What should I do to the MyString.java to solve this problem?
source code: MyString.java
public class MyString
{
private char[] letters;
public MyString( String other )
{
letters = new char[other.length() + 1];
for(int i = 0; i < other.length(); i++)
{
letters[i] = other.charAt(i);
}
}
public MyString( MyString other )
{
letters = new char[other.length() + 1];
for(int i = 0; i < other.length(); i++)
{
letters[i] = other.letters[i];
}
}
public int length()
{
int i = 0;
while(letters[i] != 0)
{
i++;
}
return i;
}
public char charAt(int index)
{
return letters[index];
}
int compareTo(MyString other)
{
int i = 0;
while(this.letters[i] == other.letters[i])
{
if(i == this.length() && i == other.length())
{
return 0;
}
i++;
}
if(this.letters[i] > other.letters[i])
{
return 1;
}
else
{
return -1;
}
}
public boolean equals(MyString other)
{
if(this.compareTo(other) == 0)
{
return true;
}
else
{
return false;
}
}
public int indexOf(int startIndex, char ch)
{
for(int i = startIndex; i <= this.length() - 1; i++)
{
if(ch == letters[i])
{
return i;
}
}
return -1;
}
public int indexOf(MyString other)
{
char ch = other.letters[0];
int c = 0;
for(int i = c; i <= this.length() - other.length() - 1; i++)
{
c = indexOf(i, ch);
int j = 0;
while(j < other.length() && (other.letters[j] == this.letters[c + j]))
{
j++;
}
if(j == other.length())
{
return c;
}
}
return -1;
}
public String toString()
{
String word = "";
for(int i = 0; i < this.length(); i++)
{
word += letters[i];
}
return word;
}
}
MyStringTester.java
public class MyStringTester
{
public static void main( String[] args )
{
MyString s1 = new MyString("Hello World");
System.out.println( "s1=" + s1 );
MyString s2 = new MyString(s1);
System.out.println( "s2=" + s2 );
if ( s1.equals( s2 ) )
System.out.println( s1 + " identical to " + s2 );
else
System.out.println( s1 + " not identical to " + s2 );
MyString s3 = new MyString( "GoodBye World" );
System.out.println( "s3='" + s3 + "' and its length is: " + s3.length() );
if ( s1.equals( s3 ) )
System.out.println( s2 + " identical to " + s3 );
else
System.out.println( s1 + " not identical to " + s3 );
s1=new MyString("albert"); s2=new MyString("alpha");
System.out.println( "s1=" + s1 + " s2=" + s2 );
System.out.println( "s1.compareTo(s2) ==> " + s1.compareTo(s2) ); // since "albert" < "alpha" returns -1
s1=new MyString("zebrano"); s2=new MyString("zebra");
System.out.println( "s1=" + s1 + " s2=" + s2 );
System.out.println( "s1.compareTo(s2) ==> " + s1.compareTo(s2) ); // since "zebrano" > "zebra" returns 1
s1=new MyString("cattle"); s2=new MyString("catty");
System.out.println( "s1=" + s1 + " s2=" + s2 );
System.out.println( "s1.compareTo(s2) ==> " + s1.compareTo(s2) ); // since "cattle" < "cattle" returns 1
s1=new MyString("pneumoencephalographically"); s2=new MyString("pneumoencephalographically");
System.out.println( "s1=" + s1 + " s2=" + s2 );
System.out.println( "s1.compareTo(s2) ==> " + s1.compareTo(s2) ); // returns 0 they are same string value
System.out.println( "[4]'th letter of " + s3 + " is " + s3.charAt(4) );
MyString key = new MyString("rld");
System.out.println( key + " found in " + s3 + " at index: " + s3.indexOf( key ) );
// SAME THING BUT WITHOUT USING A MyString VAR FOR THE KEY
System.out.println( new MyString("Goo") + " found in " + s3 + " at index: " + s3.indexOf( new MyString("Goo") ) );
System.out.println( new MyString("Bye") + " found in " + s3 + " at index: " + s3.indexOf( new MyString("Bye") ) );
System.out.println( new MyString("zorp") + " found in " + s3 + " at index: " + s3.indexOf( new MyString("zorp") ) );
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
