Results 1 to 14 of 14
  1. #1
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    2

    Question Search spetific string throught the arrays?

    Hi, thanks for reading this!
    SO I would like to know how to search a string through the variables like this:

    Java Code:
    String a[] = new String[3];
    a[0] = "bla";
    a[1] = "lol";
    a[2] = "hello";
    I want it to search the word "lol" and tell in which array is it located.

    Help please?

    If I should use the for loop then I know how to do it, I am just asking if there is an easier and shorter way?
    Last edited by Lionlev; 06-07-2012 at 03:17 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Search spetific string throught the arrays?

    Moved from Advanced Java. Nothing advanced about the question.

    Using a for loop is the correct approach.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    2

    Default Re: Search spetific string throught the arrays?

    Quote Originally Posted by DarrylBurke View Post
    Moved from Advanced Java. Nothing advanced about the question.

    Using a for loop is the correct approach.

    db
    Oh ok thanks!

  4. #4
    SirDagdlahan is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Search spetific string throught the arrays?

    Not really. The only other route I can think if would be recursion, but you're basically recreating a for loop then.
    Without the usual benefits of shortening code that recursion provides (I'm assuming you want the index in the array the string appears, and that complicates things.)
    Java Code:
    String goal = "lol";
    int i = 0;
    for(; i < a.length; i++){
      if(a[i].equals(goal))
        break;
      else if(a.length - 1 == i){
        i = -1;
        break;
      }
    }
    System.out.println(i);
    It will print -1 if not found.
    Last edited by SirDagdlahan; 06-07-2012 at 07:49 PM. Reason: Forgot to include break

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default Re: Search spetific string throught the arrays?

    @SirDagdlahan Did you test the code you posted? What does it print?
    When is the if condition on line 6 true?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    2

    Default Re: Search spetific string throught the arrays?

    No help needed anymore, I just used a while loop..

  7. #7
    SirDagdlahan is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Search spetific string throught the arrays?

    Oh, that was just something I felt was needed (for completeness)
    If nothing is found, the index printed is -1, similar to that of most java searches. If we are at the last possible index (length - 1) and we didn't find it, we won't find it in the array. Therefore, it isn't in there, show -1 to signify.
    Edit: Ah, I see. I set it to negative one, but forgot to expand it and break. My apologies.
    Last edited by SirDagdlahan; 06-07-2012 at 07:45 PM.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

    Default Re: Search spetific string throught the arrays?

    You forgot the ultimate powers of the moderators; your original post (before you edited it) read:

    Java Code:
    else if(a.length == i - 1) i = -1;
    and that's what Norm woke up; no excuses please.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default Re: Search spetific string throught the arrays?

    It'd be better if you tested code before posting it.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: Search spetific string throught the arrays?

    For loop is good enough.

    Java Code:
     public static void main(String[] args) {
            String a[]={"bla", "lol","hello"} ;
            int i=0;
            for(String _a : a) {
            i++;
             if (_a.equals("lol")) System.out.println("Found it at :" + i);}

    Ksharp

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Search spetific string throught the arrays?

    @Ksharp: Your formatting is atrocious. Read and digest the Coding Conventions for the Java Programming Language.

    Second, when an index is required within a loop, there's absolutely no point in using an enhanced-for loop.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: Search spetific string throught the arrays?

    Thanks, DB
    help to adjust my code.
    You are right, I just want to type less words. That is all. :)


    Ksharp

  13. #13
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    2

    Default Re: Search spetific string throught the arrays?

    How can I close the topic? I keep getting answers when I already said that I don't need help on this anymore..

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

    Default Re: Search spetific string throught the arrays?

    I'll close it for you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 03-11-2010, 12:49 AM
  2. Replies: 1
    Last Post: 03-31-2009, 03:15 AM
  3. Replies: 1
    Last Post: 10-02-2008, 03:06 AM
  4. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  5. Problems sending file throught TCP sockets
    By Nite in forum Advanced Java
    Replies: 2
    Last Post: 08-04-2007, 09:01 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
  •