0

im doing a program where i need to return the index of MyString of the first occurence. idk how to use the indexof in this situation. i think it might be something like ch = indexof[] but im not sure. im still new at this and the api didnt help me much

/*********************************************************************************
     This program will create classes that do similar operations to the Java String

     Javier Perez
     csc110
     11/5/12
    *********************************************************************************/
    package string.assignment;
    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 )
        {
            ch = ch.indexOf();

        }
        return index; 


    }
5
  • 1
    Maybe try the documentation? Commented Nov 13, 2012 at 5:21
  • im only halfway done but it does compile Commented Nov 13, 2012 at 5:22
  • 1
    @javip.. No your current code would not compile. Your last return statement is outside your method indexOf. May be you posted it wrong here. Commented Nov 13, 2012 at 5:25
  • @Xymostech.. No need of documentation of string.indexOf here. OP is implementing it using character array. Commented Nov 13, 2012 at 5:31
  • that api was alot more helpful then then api 7 Commented Nov 13, 2012 at 5:31

3 Answers 3

1

For fetching the index of a certain character from your array, you would have to iterate over the array, and compare each character with the one you want to check. If any character matches, return the index immediately.

Also, your last return statement should be inside your method, else your code would not compile.

So, your indexOf method in its simplest would be like this: -

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
}
Sign up to request clarification or add additional context in comments.

Comments

1

Iterate your array of characters, match the desired character against the character at index in the array. If match found, not the position in the index variable which has default value as -1 and break the loop. Return the index value from the method.

     public int indexOf( char ch ){
        int index = -1;
        for(int i=0; i < array.length; i++){
         if(array[i] == ch){
             index = i;//first match found, stop searching
             break;
         }
       }
       return index; 
     }

This way, it returns the first matching position of the character in the string's character array, otherwise returns -1.

Comments

1

If it does indeed compile then your cut and paste is bad - what is that return index doing at the end?

Anyway - not I've used your own charAt rather than just a char compare.

    public int indexOf( char ch )
    {
        int result = -1;
        for(int i = 0; i <  size; i++) {
            if (charAt(i) == ch) {
                result = i;
                break;
            }
        }
        return result;

    }

Comments

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.