Hey new to the site, but I am in my first CS class and for this project we had to implement our own class for manipulation with string called MyString. I am having problems with the methods startsWith and substring. Here's my code:
import java.util.Scanner; public class MyString { private char [] string; MyString(char [] initString){ string = new char[initString.length]; for (int i = 0; i < initString.length; i++){ string[i] = initString[i]; } } MyString(String initString){ string = new char[initString.length()]; for (int i = 0; i < initString.length(); i++){ string[i] = initString.charAt(i); } } public int length(){ return string.length; } public boolean isEmpty(){ if (length() == 0){ return true; } else return false; } public char charAt(int index){ if (index < 0 || index >= string.length){ throw new IndexOutOfBoundsException(); } return string[index]; } public boolean startsWith(MyString prefix){ int count = 0; for (int i = 0; i < string.length; i++){ if (string[i] == prefix[i]){ count++; } if (count == prefix.length){ return true; } else if (prefix.isEmpty()){ return true; } else if (prefix > string.length){ return false; } else return false; } } public int indexOf(int ch){ for (int i = 0; i < string.length; i++){ if (string[i] == ch){ return i; } } return -1; } public int indexOf(int ch, int fromIndex){ for (int i = fromIndex; i < string.length; i++){ if (string[i] == ch){ return i; } } return -1; } public MyString substring(int beginIndex){ if (beginIndex < 0){ throw new IndexOutOfBoundsException(); } if (beginIndex > string.length){ throw new IndexOutOfBoundsException(); } MyString newString = new MyString(); newString.length = newString.length - (beginIndex++); newString.string = new char [newString.length]; for (int i = 0; i < newString.length; i++){ newString.string[i] = this.string[beginIndex++]; } return newString; } }