|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Problms with boolean trying to string compare
in the FindPedalBoat and FindRowBoat methods on these lines ( if(myPedalBoat.getHired()=="False"); // string compare) there is an error (variable myPedalBoat might not have been initialised) and im not sure how to fix it any advice would be great, also the closing brackets at the end of these methods have an error saying return statement needed again any advice would be great, thanks.
import java.util.*; public class BoatHireCompany { private ArrayList <PedalBoat> thePedalBoats; private ArrayList <RowBoat> theRowBoats; private int numRowBoats; private int maxRowBoats; private int numPedalBoats; private int maxPedalBoats; public BoatHireCompany (int numR, int numP) { theRowBoats = new ArrayList <RowBoat> (maxRowBoats); thePedalBoats = new ArrayList <PedalBoat> (maxPedalBoats); maxRowBoats = numR; maxPedalBoats = numP; numRowBoats = 0; numPedalBoats = 0; } public void HireRowBoat() { RowBoat r = new RowBoat("ewe", "asfa", 3, 4); r.setHired(true); theRowBoats.add(r); } public void HirePedalBoat() { PedalBoat p = new PedalBoat("fasf", "asdasd", 23, 5); p.setHired(true); thePedalBoats.add(p); } public boolean addRowBoat( RowBoat newRowBoat ) { if ( numRowBoats == maxRowBoats ) { return false; } else { theRowBoats.add(newRowBoat); numRowBoats++; return true; } } public boolean addPedalBoat( PedalBoat newPedalBoat ) { if ( numPedalBoats == maxPedalBoats ) { return false; } else { thePedalBoats.add(newPedalBoat); numPedalBoats++; return true; } } 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; } } public PedalBoat findPedalBoat() { PedalBoat myPedalBoat; // loop through list of boats for ( int i=0; i < numPedalBoats; i++) { if (thePedalBoats.get(i) instanceof PedalBoat) { PedalBoat myPedalBoats = (PedalBoat)(thePedalBoats.get(i)); if(myPedalBoat.getHired()=="False"); // string compare { System.out.println(myPedalBoats.toString()); } } } // test each boat - if it is a pedal boat, print it out } public RowBoat findRowBoat() // finds all available boats of a certain type input by user { RowBoat myRowBoat; // loop through list of boats for ( int i=0; i < numRowBoats; i++) { if (theRowBoats.get(i) instanceof RowBoat) { RowBoat myRowBoats = (RowBoat)(theRowBoats.get(i)); if(myRowBoat.getHired()=="False"); // string compare { System.out.println(myRowBoats.toString()); } } } // test each boat - if it is a pedal boat, print it out } |
|
#2
|
|||
|
|||
|
If you take a closer look at your code:
public PedalBoat findPedalBoat() { PedalBoat myPedalBoat; // loop through list of boats for ( int i=0; i < numPedalBoats; i++) { if (thePedalBoats.get(i) instanceof PedalBoat) { PedalBoat myPedalBoats = (PedalBoat)(thePedalBoats.get(i)); if(myPedalBoat.getHired()=="False"); // string compare { System.out.println(myPedalBoats.toString()); } } } There are at least two major things wrong: * The "might not be initialized" error is because you are trying to access the myPedalBoat variable (which is indeed not initialized in your code, only declared in the first line of the method). Maybe you want to access the myPedalBoats variable (not the "s" at the end of the name... * You should *never* compare strings with ==, that produces an object comparison, under which two strings will never be equal. You have to use the equals() or equalsIgnoreCase() methods. For example: if(myPedalBoat.getHired().equals("False")) |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Problms with boolean trying to string compare |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|