Help with MyString class!
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:
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;
}
}
Re: Help with MyString class!
Things I noticed:
Code Java:
MyString(char [] initString) { // Why is this package private?
...
}
MyString(String initString) { // Again.. Why package private?
// Look at the toCharArray in String.
}
...
public boolean startsWith(MyString prefix){
...
if(count == prefix.length)
return true;
}
// The If-statements after that are redundant. If the prefix is empty, length is 0,
// So count is going to equal prefix.length. Second, if the prefix is larger
// than the string, than count != prefix.length. [It will be less than]
else
return false;
}
And as for your substring method, you should read: Assignment, Arithmetic, and Unary Operators (The Java™ Tutorials > Learning the Java Language > Language Basics) because
it looks like your confusing ++beginIndex and beginIndex++
Other than that, what are the specific errors/bugs that your getting?
Re: Help with MyString class!
I have fixed the startsWith method and it works properly now, but I am having problems with the substring still... my assignment says it is supposed to return a new string that is a substring of this string. the substring begins with the character at the specified index and extends to the end of this string. and throws and exception if beginIndex is negative or larger than the length of this String object.
Re: Help with MyString class!
Any time you use the var++ operator, it will add one to the value and save it.
Code Java:
int[] array = new int[] { 1, 2, 3, 4 };
int index = 0;
int val2 = array[index++]; // index is now 1.
int val3 = array[++index]; // Adds one to the index, but does not save it.
Knowing that, re-evaluate the logic of your substring method.