Results 1 to 9 of 9
  1. #1
    rokit boy is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Finding a string in an array.

    Let's say I have an Array of strings:
    Java Code:
    String[] a;
    a = new String[3]
    a[0] = "a"
    a[1] = "b"
    a[2] = "c"
    and let's say I have another string:
    Java Code:
    String stuff = "b"
    How can I make it so I have it that when there is a "b" in the array then it will do something?
    Like print "b is in the array"?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Finding a string in an array.

    Have you tried anything yet? If so, can you show us as well as any problems with the code? If not, what do you think might work?

  3. #3
    rokit boy is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: Finding a string in an array.

    I have tried some stuff, but nothing worked.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Finding a string in an array.

    Quote Originally Posted by rokit boy View Post
    I have tried some stuff, but nothing worked.
    Let's see what you've tried, and then let's figure out why it didn't work.

  5. #5
    rokit boy is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: Finding a string in an array.

    Well I tried random stuff like a["b"] but of course there is so many reasons why that is incorrect.
    Then I searched through methods. Nothing.
    Then I tried something clever from my side:
    Java Code:
    for (String thing : a) {
    	if (a.matches(thing)) {
    		print("Found it!");
    	}
    }
    but a wild NullPointerException has appeared from the wild grass and killed my pokemon.
    Why the Exception?
    Edit: I think I know what I did wrong.
    Replace the String thing with stuff, hopefully it will work.

  6. #6
    rokit boy is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: Finding a string in an array.

    Found it out!
    Use it if you want.
    Java Code:
    for (String thing : a) {
        if (thing == b) {
    	print("Found it!");
        }
    }

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

    Default Re: Finding a string in an array.

    Moved from Advanced Java

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

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

    Default Re: Finding a string in an array.

    Never use == to compare Strings or any other reference types. Use the .equals(...) method.

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

  9. #9
    rokit boy is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: Finding a string in an array.

    Yep, i changed it in my original code.

Similar Threads

  1. Replies: 2
    Last Post: 03-18-2012, 04:09 AM
  2. Finding Character at a Certain Index in a String?
    By Yaviel in forum New To Java
    Replies: 3
    Last Post: 02-08-2012, 11:59 PM
  3. Help with TreeSet and finding longest string
    By JavaTheHut in forum New To Java
    Replies: 3
    Last Post: 05-04-2011, 10:23 PM
  4. Finding the Mode in An Array
    By carlodelmundo in forum New To Java
    Replies: 23
    Last Post: 10-31-2010, 12:44 PM
  5. Finding and storing a word from a string builder
    By ao241 in forum Advanced Java
    Replies: 3
    Last Post: 06-28-2010, 12:46 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
  •