Results 1 to 18 of 18

Thread: MyString Help

  1. #1
    mritson is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default MyString Help

    Hey new to the site and wasn't sure where to post this, 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:
    Java 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
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default Re: MyString Help

    Java Code:
    public boolean startsWith(MyString prefix){
        if(!prefix.isEmpty() && prefix <= this.length()){
           int i=0;
           for(i;i<prefix.length();i++){
               if(string[i] == prefix.charAt(i)){
                    continue;
               }else{
                  break;
               }
          }
          if(i==prefix.length()){
              return true;
          }
        }
         return false;
    }

    I think this will work although I haven't checked it....

  3. #3
    mritson is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: MyString Help

    yeah i fixed my startsWith method and it works now, but I am still having problems with the substring one. any ideas?

  4. #4
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default Re: MyString Help

    First tell me did my method worked or not?

    Second, as an advice, In your substring method first create a character array ( char[] ) having the length Main String minus begin index. Then iterate from begin index to the length of the main string and simultaneously assigning each character in the char array in place...... and then construct a new object of mystring using the constructor and return that object...

  5. #5
    mritson is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: MyString Help

    your method works well.

    but I'm confused by how I should iterate from begin index to the length and such
    Last edited by mritson; 04-11-2012 at 08:20 AM.

  6. #6
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default Re: MyString Help

    Take this for hint

    Java Code:
        for(beginIndex;beginIndex < this.length();beginIndex ++){
            //Your logic here
        }
    check for expected string with returned string and then correct the method to get correct string........

  7. #7
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default Re: MyString Help

    Oh...I made a mistake in the first method..Check this

    Java Code:
       public boolean startsWith(MyString prefix){
                boolean status = false;
                 if(!prefix.isEmpty() && prefix <= this.length()){
                     int i=0;
                      for(i;i<prefix.length();i++){
                            if(string[i] == prefix.charAt(i)){
                                status = true;
                                continue;
                           }else{
                               status = false;
                               break;
                           }
                       }
                  }
                  return status;
               }
    Last edited by gauravfzd; 04-11-2012 at 08:39 AM.

  8. #8
    mritson is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: MyString Help

    what am I doing wrong, im sorry this is my first CS class and i'm very confused. here's what i have tried...
    Java Code:
    public MyString substring(int beginIndex){
    
        if (beginIndex < 0){
          throw new IndexOutOfBoundsException();
        }
        if (beginIndex > string.length){
          throw new IndexOutOfBoundsException();
        }
    int startIndex = this.length() - beginIndex;
        for (startIndex; startIndex < this.length(); startIndex++){
          string[startIndex] = this.string[beginIndex + startIndex]; 
        }
        
        return startIndex;
    
      }
    Last edited by mritson; 04-11-2012 at 08:39 AM.

  9. #9
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default Re: MyString Help

    first check the corrected startWith method...

    Second, In your substring method the use of startindex is wrong.
    For example I call
    Java Code:
    MyString str = new MyString("abcdefghijk");
    str.substring(2);
    your method will return only "jk" as str.length() is 11 and beginIndex is 2 .So startIndex will be 9
    Last edited by gauravfzd; 04-11-2012 at 08:57 AM.

  10. #10
    mritson is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: MyString Help

    Ok i got it to compiles, but when i test it, it doesn't work properly. Here's my substring:
    Java Code:
     public MyString substring(int beginIndex){
    
        if (beginIndex < 0){
          throw new IndexOutOfBoundsException();
        }
        if (beginIndex > string.length){
          throw new IndexOutOfBoundsException();
        }
        
        MyString newString = new MyString("string");
        newString.string = new char [newString.length()];
        
        for (int i = 0; i < newString.length(); i++){
          newString.string[i] = this.string[beginIndex+i];
        } 
        
        return newString;
    
      }
    and here's my tester:
    Java Code:
      public static void main (String [] args){
         
        MyString tester = new MyString ("bugs bunny");
        
        /**prints out the length of the string*/
        System.out.println(tester.length());
        /**prints out if the string is empty or not*/
        System.out.println(tester.isEmpty());
        /**prints outs the character at a position*/
        System.out.println(tester.charAt(2));
        /**prints out if the string starts with the specific prefix*/
        System.out.println(tester.startsWith(new MyString("bu")));
        /**prints out the index at a certain spot */
        System.out.println(tester.indexOf(3));
        /**prints out the index at a certain spot */
        System.out.println(tester.indexOf(1));
        /**prints out the substring*/
        System.out.println(tester.substring(2));
      }
    for the substring test it prints out some 'MyString@4551dcd6'

  11. #11
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default Re: MyString Help

    Ok try to understand my code:

    Java Code:
    public MyString substring(int beginIndex){
        char[]substr = new char[this.length() - beginIndex];
        
        for(int i = beginIndex;i < this.length();i ++){
            substr[i - beginIndex] = this.charAt(i);
        }
        return MyString(substr);
        
    }
    I hope this work.....Check for deviation from the expected result(if exist). And if you like (or satisfied) you can click the star just below this post.....

  12. #12
    mritson is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: MyString Help

    yes that makes a lot of sense to me, but i am getting an error on the last line it's: Error: The method MyString(char[]) is undefined for the type MyString

  13. #13
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default Re: MyString Help

    I am just referring to the constructor that was defined by you

    MyString(char [] initString){

    }

    always use public modifiers to the Constructors
    as

    public MyString()......
    Last edited by gauravfzd; 04-11-2012 at 09:24 AM.

  14. #14
    mritson is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: MyString Help

    I understand that, but why does it not compile? what can I do to fix it?

  15. #15
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default Re: MyString Help

    Ahh.. My mistake....

    remove the last line with

    return new MyString(substr);

  16. #16
    mritson is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: MyString Help

    thank you very much!

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,255
    Rep Power
    25

    Default Re: MyString Help

    Was this also posted at: All help would be much appreciated!!
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,255
    Rep Power
    25

    Default Re: MyString Help

    Quote Originally Posted by mritson View Post
    Hey new to the site and wasn't sure where to post this, 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:
    Java 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;
      }  
    
    }
    To save the code that was deleted from other thread.
    If you don't understand my response, don't ignore it, ask a question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •