This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Search array will only return the first entered value.  RSS feed

 
Beth Elliott
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have created a menu option program with an array. One of the options is to search the array for an object.
See below method searchCar()
My code is only return the first entered object any ideas why?


 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't think that will work, try
 
Carey Brown
Bartender
Posts: 2985
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
I don't think that will work, try

Your 'a++' suggestion is the conventional approach but either will work.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I see it now:

Your inner for-loop has a body of an if-then-else. If then... you break, else you return. So the loop only executes once.

Do you mean:

And why are there two for-loops? Wouldn't one be fine?
 
Carey Brown
Bartender
Posts: 2985
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need to remove the 'break' and move the '!found' outside of the loop.
 
Carey Brown
Bartender
Posts: 2985
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooops
Change

to
 
Beth Elliott
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you !!! It now works perfectly!!!
 
Carey Brown
Bartender
Posts: 2985
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome ;-)

I hope you take the time to understand the basic flow of this search, it is one of THE most used patterns in Java.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beth Elliott wrote:I have created a menu option program with an array. One of the options is to search the array for an object.
See below method searchCar()

I think Carey's dealt with your basic problem, but part of the reason you have such a pile of code is that you're thinking about your car park as an algortihm, and not as an object, or system.

Let's get back to basics: What is a car park?

From what I can see, it's a container for a bunch of cars, each of which is identified by a license plate.
And that suggests at least three classes to me: CarPark, Car, and LicensePlate.

Another problem is that you're using arrays instead of Lists, (or indeed Sets, which would probably be preferable, since you can't have "duplicate" Cars).

However, assuming you're stuck with having to use arrays, wouldn't it be nice to have a generic function that can find an object in ANY array? ie, something like:
  public static final <T> indexOf(T object, T[]array) { ...
?
Well, what about:then you could develop on that, viz;
But, as you can probably see, they rely on your "objects" having a properly implemented 'equals()' method, which means that your Car class needs to implement one.
So: How do we know if two Cars are "equal"? Simple: when their license plates are "equal".
So how do we know that? When they have the same non-blank characters (or digits), in the same order and ignoring case.

And, from that, I get something like:And NOW you're on your way to an Object-Oriented solution.

Do you also see that a licence plate is NOT just a String? First, it's a String with very specific comparison requirements; but it might also need validation to weed out things like bad or racist language (even if it's inadvertent).

I leave the rest up to you, but hopefully it gives you something to think about. Java works best with objects, not code; and the sooner you can start to think about the objects and classes in your problem, the cleaner your code will be. And all that initial work will also help you when you do decide to use collections instead of arrays.

I should add that the above is only ONE way to do it; there are plenty of others. But when you're starting out on a problem like this, try to think about what, not how.

HIH

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!