Results 1 to 8 of 8
  1. #1
    The Hawk is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default Searching the first occurence

    Hi everybody,

    i should write a methode, which gives the first occurence of a substring in a string with an index.

    If the string starts with the substring so the index must be 0, if the substring does not exist in the string so the index is -1. If the substring.length is 0 so the index is 0.

    The method is:

    Java Code:
    public static int getFirstOccurence(char[] str, char[] substr){
                      //TO DO
    }
    The strings are changed into the array. In this method we are working with arrays which have charakters. (I've already wrote the methode to change strings into the array...)

    Can anybody help me? I don't know how to search an array, in an other array?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by The Hawk View Post
    Hi everybody,

    i should write a methode, which gives the first occurence of a substring in a string with an index.

    If the string starts with the substring so the index must be 0, if the substring does not exist in the string so the index is -1. If the substring.length is 0 so the index is 0.

    The method is:

    Java Code:
    public static int getFirstOccurence(char[] str, char[] substr){
                      //TO DO
    }
    The strings are changed into the array. In this method we are working with arrays which have charakters. (I've already wrote the methode to change strings into the array...)

    Can anybody help me? I don't know how to search an array, in an other array?
    Better leave those Strings as Strings and use the indexOf() method (read the API documentation for the String class).

    kind regards,

    Jos

  3. #3
    The Hawk is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    Thank you for your answer,

    but this is an homework and i should do it with arrays. Is there any other ideas, or some tipps?

  4. #4
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    So we are solving your homework?
    please, learn something...
    but anyway make a for loop for al the charachter
    check if the character is the first character of the searched string
    if so, check if the next charachter of the string is the next character of the searched string
    (another for loop)
    hope this helps and that you solve youre homework by yourself

  5. #5
    The Hawk is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    thanks dinosep,

    i don't want you to solve my homework, it's only a little part of whole homework. I couln't do it. I needed some help. Every tipp can be really helpfull. :)

    i'll try your tip. thanks again :)

  6. #6
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    ok,tell me if it works.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    The OP can smuggle a bit: look in the directory where the JDK is stored; it contains a file src.zip; it contains the source code for all the publicly avaialable classes. The source for the String class is also in there and that class contains the indexOf( ... ) method. Don't tell them I told you ;-)

    kind regards,

    Jos

  8. #8
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    thanks jos, what you told there was something i didn't know and surely are going to use
    rep+

Similar Threads

  1. Searching XML file using DOM
    By simon in forum XML
    Replies: 5
    Last Post: 03-30-2009, 01:27 AM
  2. count occurence of word in a line of text
    By sinyi88 in forum New To Java
    Replies: 19
    Last Post: 02-28-2009, 07:37 AM
  3. Problem with searching while using GUI
    By BHCluster in forum AWT / Swing
    Replies: 1
    Last Post: 04-24-2008, 06:56 PM
  4. Searching an arraylist
    By adelgado0723 in forum New To Java
    Replies: 1
    Last Post: 04-15-2008, 01:09 PM
  5. searching
    By nalinda in forum New To Java
    Replies: 3
    Last Post: 12-06-2007, 02:56 AM

Posting Permissions

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