• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Searching in an Array  RSS feed

 
Ste Vet
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Whenever I run this method I get not found, even thou I know the word Im searching for is in the array
What is wrong with my code?

First post

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

There are a few odd things with your code. First of all, you have a loop (line 6). In line 7 you get the Bil at a specified index. But you're not checking if that is the Bil you are looking for; instead, in line 8, you check if the string searchString exists in the collection biler.

That's the main error. biler contains Bil objects, not strings, so the statement in line 8 will always return false.

You probably meant something else in line 8. I guess that there you have to compare the current Bil that you got in line 7 to searchString somehow. Probably class Bil has a member variable that contains the name of the Bil, and you'll want to compare that to searchString.
 
Ste Vet
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see, thanks for the quick answar

The arraylist contains objects of the class Bil

I just started with java so Im kinda new to it all
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, biler is not an array, because you can't call methods such as size() and get(...) on an array. It's probably an ArrayList.
 
Ste Vet
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList yes, my bad.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ste Vet wrote:What is wrong with my code?

I think others have dealt with the logic errors, but there are a few other things that you probably should think about:

1. The method is tightly coupled for two reasons:
(a) The List it searches is defined outside the method.
(b) The result of the search is only used inside the method.
and for that reason, it won't work anywhere but in your current class (once it's correct ).

If you passed your list in as a parameter and returned the result (perhaps the index where the string was found?), you could use it anywhere you needed such a search, and you would hardly have to change any code. Then it would look something like:

public static final int searchBil(String searchString, List<Bil> biler) { ...

Making it static allows it to be called from anywhere, and making it final prevents anyone else from accidentally creating a method with the same name.

2. All Java Collections are Iterable (see java.lang.Iterable), and can therefore use the 'for-each' style loop, viz:
for (Bil bil: biler) { ...

3. You can use a 'break' statement to break out of any loop:
Hopefully that's given you a few things to think about while you're getting it right.

Winston
 
ghani khan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i mean to say that simpler form of this program is given on webpage
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
That link has some poor code in, and I wouldn’t recommend it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!