0

need to create a int length() method where the array is outside of the method . Was thinking of doing a for loop for (int i = 0; i < array.length; i++)...but im not sure help please. on API it looks easy but the array is not in the method itself so idk how to do it.

public class MyString
{

    private char[] array;
    private int size;
    private int max;

    public MyString()
    {
        array = new char[25];
        max = 25;
    }
    public void setString(String newString)
    {
     if(newString.length() > 25)
     {
         System.out.println("/nEnter a number equal or less than 25 " );

     }
     else
     {
      for(int i=0; i < newString.length(); i++)
      {
        array[i] = newString.charAt(i);
      }
     }
    }


    public String toString()
    {
        return new String(array);
    }

    public char charAt(int index)
    {
        return array[index];
    }

    public boolean contains(char ch)
    {
        for(char c: array)
        {
            if(c == ch) return true;
        }
        return false;
     }
     public int indexOf( char ch )
     {
        for (int i = 0; i < array.length; i++)
        {
           if (array[i] == ch)
           {
               return i;   // Character found, return current index
           }
         }
            return -1;    // Character not found. Return -1
      }
      public int length()
      {
3
  • is this homework? If yes, please add a tag for it homework Commented Nov 13, 2012 at 6:58
  • 2
    @FahimParkar No, The homework tag is deprecated Commented Nov 13, 2012 at 6:59
  • @Andreas : Ohh... thanks for sharing that. I know un-aware. Also I don't understand, when TAG is deprecated, why it still exists in SO? Commented Nov 13, 2012 at 7:09

2 Answers 2

1

In your examplearray.length always will return 25. If you want the length of array with values, use :

  public int length(){
       int i = 0;
       for (char c : array) {               
           if (c == '\u0000')
               return i;  
           i++;         
       }
       return array.length;
  }
Sign up to request clarification or add additional context in comments.

Comments

0

I am not sure if I get your question completely right, but

public int length() {
   return array.length;
}

should do it. An array in java has a length member which contains the array length.

Note that this returns the Array length which will always be 25 in the code from your question.

If you want the String length, either use the approach from @Grisha, or use the size member which is already in your code but not used yet:

public MyString() {
   array = new char[25];
   max = 25;

   size = 0;
}

public void setString(String newString) {
   if(newString.length() > 25) {
      System.out.println("/nEnter a number equal or less than 25 " );
   } else {
      size = newString.length();

      for(int i = 0; i < size; i++) {
          array[i] = newString.charAt(i);
      }
   }
}

public int length() {
   return size;
}

This would avoid a loop with linear complexity (length() would be O(1) and not O(n)).

9 Comments

wow is that it? i thought it would be more work. but pretty much just need to get the length of the array
need to create a int length(). I think OP want to create length method and not use the default one.
Ohh.. OP wanted what you said.
@codeMaker Yes, I was thinking that way also, but than the answer would have been return 25. By returning array.length, you still have a single position in the code where you can adjust the maximum length.
@codeMaker Ahhh, got it - OP wants to return the String length, not the array length.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.