Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
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  
Old April 25th, 2008, 08:28 AM
albear albear is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 10 albear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 32 m 27 sec
Reputation Power: 0
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
}

Reply With Quote
  #2  
Old May 7th, 2008, 04:02 PM
ricardoz ricardoz is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Location: Montevideo, Uruguay
Posts: 9 ricardoz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 34 sec
Reputation Power: 0
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"))

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Problms with boolean trying to string compare


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway