• Post Reply Bookmark Topic Watch Topic
  • New Topic

Searching string in the array  RSS feed

 
umut uzumcu
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
umut uzumcu
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!