Results 1 to 4 of 4
  1. #1
    VIhuri is offline Member
    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Default Need help with creative use of indexOf-command

    I am tasked to create a program that is given two string variables and it is supposed to find the first variable's second manifestation in the second string. For example if first string is "at" and the second is "kattimatti" it is supposed to print out 6. If the first string isn't to be found in second string twice, it's supposed to give out an error message.

    Now finding the first string in the second string for the first time is simple enough with indexOf - command and with if-else structure it's simple to give out the error message or the found index.

    However, I cannot find any logical way to find the SECOND time first string is found in second string. In "at" and "kattimatti" example I can only print out "1" which is the place first "at" starts.

    Should I try some loop-command, either for or while?

    Thank you for any incoming help.

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

    Default Re: Need help with creative use of indexOf-command

    No loops or anything; just read a bit further in the API documentation (hint there's another indexOf( ... ) method that starts at a certain position in the String).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    VIhuri is offline Member
    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Default Re: Need help with creative use of indexOf-command

    Thanks, got it working!

    Only slight problem is that when there is only one occurrence of string a in string b, it prints out the index rather than the error message. I tried to give the if-method the condition that d > c (c being the first index found with indexOf-method, d being the second one starting it's search from c+1) My logic is that if both found the same index-number the condition of d > c would not be met.

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

    Default Re: Need help with creative use of indexOf-command

    That's just bit of programming: if c < 0 your String didn't occur in the other String at all; if c >= 0 and d < 0 your String only occurred once in the other String, otherwise d > c ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Help with indexof please...
    By fatabass in forum New To Java
    Replies: 7
    Last Post: 02-07-2012, 10:36 PM
  2. 3-week vacation creative project
    By MGhareeb in forum New To Java
    Replies: 3
    Last Post: 01-24-2011, 06:24 PM
  3. Creative graduate programmers needed!
    By kirstyjenifer in forum Jobs Offered
    Replies: 2
    Last Post: 08-25-2009, 03:05 PM
  4. Looking for creative graduate geeks!
    By kirstyjenifer in forum Jobs Discussion
    Replies: 2
    Last Post: 07-17-2009, 06:33 PM
  5. Help regarding indexOf
    By gauravj in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 02:12 PM

Posting Permissions

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