Results 1 to 9 of 9
Thread: Finding a string in an array.
- 03-18-2012, 04:26 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
Finding a string in an array.
Let's say I have an Array of strings:
and let's say I have another string:Java Code:String[] a; a = new String[3] a[0] = "a" a[1] = "b" a[2] = "c"
How can I make it so I have it that when there is a "b" in the array then it will do something?Java Code:String stuff = "b"
Like print "b is in the array"?
-
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?
- 03-18-2012, 04:49 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
Re: Finding a string in an array.
I have tried some stuff, but nothing worked.
-
Re: Finding a string in an array.
- 03-18-2012, 05:01 AM #5
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
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:
but a wild NullPointerException has appeared from the wild grass and killed my pokemon.Java Code:for (String thing : a) { if (a.matches(thing)) { print("Found it!"); } }
Why the Exception?
Edit: I think I know what I did wrong.
Replace the String thing with stuff, hopefully it will work.
- 03-18-2012, 05:06 AM #6
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
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!"); } }
- 03-18-2012, 06:36 AM #7
Re: Finding a string in an array.
Moved from Advanced Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-18-2012, 06:37 AM #8
Re: Finding a string in an array.
Never use == to compare Strings or any other reference types. Use the .equals(...) method.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-18-2012, 06:38 AM #9
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
Similar Threads
-
Finding all the contents of the string after a specified letter
By rokit boy in forum Advanced JavaReplies: 2Last Post: 03-18-2012, 04:09 AM -
Finding Character at a Certain Index in a String?
By Yaviel in forum New To JavaReplies: 3Last Post: 02-08-2012, 11:59 PM -
Help with TreeSet and finding longest string
By JavaTheHut in forum New To JavaReplies: 3Last Post: 05-04-2011, 10:23 PM -
Finding the Mode in An Array
By carlodelmundo in forum New To JavaReplies: 23Last Post: 10-31-2010, 12:44 PM -
Finding and storing a word from a string builder
By ao241 in forum Advanced JavaReplies: 3Last Post: 06-28-2010, 12:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks