Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with MyString class!

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    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;
      }  
     
    }


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Help with MyString class!

    Things I noticed:

    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?

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Help with MyString class!

    Any time you use the var++ operator, it will add one to the value and save it.

     
    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.

Similar Threads

  1. Replies: 7
    Last Post: July 21st, 2011, 02:29 PM
  2. In a class create an array list of elements of another class, help!
    By LadyBelka in forum Collections and Generics
    Replies: 3
    Last Post: May 4th, 2011, 05:00 PM
  3. Replies: 3
    Last Post: April 13th, 2011, 03:30 PM
  4. Help requested - testing a class with a tester class, no methods allowed.
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 10:40 PM
  5. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM