| Author |
Searching string in the array
|
umut uzumcu
Ranch Hand
Joined: Jan 10, 2007
Posts: 43
|
|
How can I find the String in the Array in Java. eg.; String[] s=new String("Java Programming" String searched="Pro"; Im searching the Pro in the Java Programming.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
You can't assugn a String directly to an array reference. You actually have to instantiate the string array object for the "s" variable. It also looks like your question is not even related to arrays -- instead, you are asking how to search a string for a substring. The easiest way to find a substring in a string, is to use the indexOf() method. Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
John Bartlett
Ranch Hand
Joined: Jan 25, 2006
Posts: 116
|
|
Hey, This is a way to search for a character sequence e.g. Pro, The String class has some very useful methods. Hope that helps, John [ January 10, 2007: Message edited by: John Bartlett ]
|
 |
umut uzumcu
Ranch Hand
Joined: Jan 10, 2007
Posts: 43
|
|
Actually here is the question i m tring to solve Does anybody has any idea? Implement a method that receives the following parameters: 1. an array of Strings 2. a String called "searched" The method looks for the "searched" string and returns its index. If the string isn't found, the method returns -1. You can assume that the array doesn't contain null values. Find a string in an array package com.javala.exercise; public class FindStringInArrayExercise { public int findString(String[] elements, String searched) { //write code here } }
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9950
|
|
|
If I handed you a box full of index cards with names written on them, then handed you another card with the name "Fred" on it, how would you find out if "Fred" was in the box?
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
umut uzumcu
Ranch Hand
Joined: Jan 10, 2007
Posts: 43
|
|
If I handed you a box full of index cards with names written on them, then handed you another card with the name "Fred" on it, how would you find out if "Fred" was in the box?
From the index numbers?
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9950
|
|
Tell me what would do, in words, not programming terms. would you erase everyting on every card? would you look at one, and stop? would you burn them? Note that i never said the cards had an index number. they're just cards in the box. I'm trying to get you to think about the logical steps involved. Once you can tell me the steps, we'll either start writing code, or analyzing those to see if they need to be broken down more.
|
 |
umut uzumcu
Ranch Hand
Joined: Jan 10, 2007
Posts: 43
|
|
Tell me what would do, in words, not programming terms. would you erase everyting on every card? would you look at one, and stop? would you burn them? Note that i never said the cards had an index number. they're just cards in the box.
I start to check them 1-by-1 to find out if the Fred is in the box or not.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9950
|
|
BINGO!!! That is correct. How are you going to do this? Are you going to pull out one at random, then throw it back in the box? are you going to only look at 5 then stop? 10? You might want to also start thinking about testing this method. Maybe you could write a main() method that calls this. that's a good place to start, too. write a main method that creates a String array with some Strings in it, and then another String. pass these into the method. do that much and get it working before writing too much more code. we'll keep progressing in these baby steps. that is the best way to write code, whether for a small, 1 person school project, or working with a team of 30 developers on a major Enterprise app.
|
 |
 |
|
|
subject: Searching string in the array
|
|
|