|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
Finding object by searching for a string
i have my find method, which is meant to find a boat by searching for the idnumber of a row/pedaloat with the same string as the one i put in.
public Boat findBoat(String IDNumber) // finds all available boats of a certain type input by user { for (int i=0; i < numPedalBoats; i++) { PedalBoat idn = thePedalBoats.get(i); if (idn.getIDNumber().equalsIgnoreCase(IDNumber)) { System.out.println(thePedalBoats.toString()); // how do i get it to print out the boats details } return idn; } for (int i=0; i < numRowBoats; i++) { RowBoat idn = theRowBoats.get(i); if (idn.getIDNumber().equalsIgnoreCase(IDNumber)) { System.out.println(theRowBoats.toString()); // how do i get it to print out the boats details } return idn; } return null; } its meant to find a pedalboat or a rowboat depending on what id number i enter. RowBoat andy = new RowBoat(null, "012345", 0, false, 0, "Andy", 10, 0); System.out.println("Testing andy toString method:"); andy.displayDetails(); BoatHireCompany Bridge = new BoatHireCompany(10, 10); Bridge.addRowBoat(andy); Bridge.findBoat("012345"); so ive done this because im not sure how else to do it but it dosent work. if you need more info just tell me but any help would be great thanks |
|
#2
|
|||
|
|||
|
As a first tip, you have several return statements in the same method. That is usually a bad idea, since it obscures your code and is very error prone.
You should define a local variable and set it with what you want to return, and use a single return statement at the end of your method. Other than that, your method has a return statement inside the first "for" loop, that will prevent the rest of the method to be executed... |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Finding object by searching for a string |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|