What in the heck is goin on!!!!!  | |
October 19th, 2002, 08:52 PM
|
#1 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Pasadena, CA
Posts: 2,177
| What in the heck is goin on!!!!!
Ok...here's a snippet of code for ya. Code: //-----------------------------------------------------------------------
public String chargeBasis()
{
String temp1 = "Daily";
double temp = numDaysRented*25.99;
if (temp >= 425.00) {temp1 = "Monthly";}
else if (temp <= 363.86 && temp >= 311.88) {temp1 = "Weekly";}
else if (temp <= 181.93 && temp >= 155.94) {temp1 = "Weekly";}
return temp1;
}
//----------------------------------------------------------------------- the 'numDaysRented' variable comes from the main class where the user is prompted for the previous date they rented a car (we're modeling a rental car proggy) and then the date they enter (taken in as a string and converted to a Date type) is compared to the system date.
The problem is....for a numDaysRented of 17 it returns temp1 as being "Daily" which is NOT the case. I checked the exact code in its own little test class. It worked perfectly. this is the test class: Code: class Tester
{
public static void main(String []args)
{
String temp1 = "Daily";
double temp = 18*25.99;
//String tempType = carType.substring(0,1);
if (temp >= 425.00) {temp1 = "Monthly";}
else if (temp <= 363.86 && temp >= 311.88) {temp1 = "Weekly";}
else if (temp <= 181.93 && temp >= 155.94) {temp1 = "Weekly";}
System.out.println(temp1);
}
} WHY IN THE &$(# Does it give me different answers!!!! The only thing I can think of is that it has to do with the numDaysRentedVariable. Any other ideas??? Im so angry right now cause its just ONE stupid little thing that is sooo not obvious.  HELP STRANGER!
__________________
YAH! I knew you'd be jealous
Last edited by Tekk : October 19th, 2002 at 08:54 PM.
|
| |
October 19th, 2002, 09:05 PM
|
#2 (permalink)
| | Where's the beef?
Join Date: Mar 2002 Location: Southwest, VA
Posts: 3,585
|
Can you print the whole class? Problems like this usually have to do with Scope.
__________________
Where's Lunch?
|
| |
October 19th, 2002, 09:22 PM
|
#3 (permalink)
| | Not Really a Member
Join Date: Oct 2001
Posts: 25,397
|
1. come on man you can come up with better variable names than temp and temp1  lol.. makes it confusing
2. Try putting an Else temp1 = "Doh! hit else" & temp to see if its just falling through and what the value of temp is (dont know what the concat operator is in java)
__________________
Helicopters don't fly; they vibrate so much and make so much noise that the earth rejects them.
|
| |
October 19th, 2002, 09:24 PM
|
#4 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Alberta, Canada
Posts: 563
|
strange, looks like it should work with numDaysRented=17
personally, I'd set a breakpoint at "double temp = numDaysRented*25.99;" and step into it to check the value in numDaysRented when it's running...
also, if it were mine, I'd change it to have the days rented passed to it by value, rather than use a global(if possible...):
public String chargeBasis(double dblNumDaysRented)
{
...some code...
}
but, that's just me!
g'luck!!
edit: good ol' quik-typin' vass beat me!!  lol I was gonna mention the temp/temp1 variables too... dunno why, but i didn't...  but I agree, temp-this/temp-that gets confusing!! Use self-documenting variables if and whenever possible!! makes a big difference!! Just try to keep them under 15 chars long!!  although, my "dblNumDaysRented" is 16... DOH!!
Last edited by ^hyd^ : October 19th, 2002 at 09:28 PM.
|
| |
October 19th, 2002, 09:30 PM
|
#5 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 1,542
|
OK, well first off you say 17 to start with and then your Tester class uses 18. Not good!
Second, are you *sure* that the value is 17 and not -17? What values does it work for?
Minor niggle: I don't like Code: if (temp <= 363.86 && temp >= 311.88) Far better to do it like this: Code: if (311.88 <= temp && temp <= 363.86) or Code: if (temp >= 311.88 && temp <= 363.86) as this is closer to the idea "temp is between values a and b"
Oh, don't use 'temp' please! It's too easily mistaken for something like 'temperature', 'temporary', etc - yes that variable is temporary but then all variables are. If you can't think of a name for a variable, call it 'foo', 'bar' etc, or just 's', 'i', 'd', etc. |
| |
October 19th, 2002, 11:26 PM
|
#6 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Pasadena, CA
Posts: 2,177
|
I got it now...
Thanks you guys. Vas..you were right, it just kept droppin through because it believed the value of numDaysRented to be 0!!! Well I figured that out. Anyway, thanks everyone for kickin my booty into recognizing it!!! hehe. And that whole 17/18 thing was a typo stanger...hehe...darn carpal tunnel fingers!
heres the finished WORKING (as of now anyway...we'll see later!) code: Code: //-----------------------------------------------------------------------
public String chargeBasis()
{
String defaultRate = "Daily";
int days = numDaysRented();
if (days >= 16) {defaultRate = "Monthly";}
else if (days >= 12 && days <= 14) {defaultRate = "Weekly";}
else if (days >= 6 && days <= 7) {defaultRate = "Weekly";}
return defaultRate;
}
//-----------------------------------------------------------------------
Last edited by Tekk : October 19th, 2002 at 11:47 PM.
|
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |