Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using 'null'

Status
Not open for further replies.

rastman222

Programmer
Nov 17, 2008
3
CA
Writing some code for a check out procedure.

When the user inputs '0', I want the code to null the zero and go back to the last balance. Hitting 0 means mistake and whatever total entered be removed from the running total.

if(itemPrice >= 0.01 & itemPrice <= 4999.99)
{
count ++;
runningTotal += itemPrice;
System.out.println("Your total is $" + runningTotal);
}//end if

else if(itemPrice == 0)
{
itemPrice = null;*error*
count --;
runningTotal -= itemPrice;
System.out.println("Removing $" + itemPrice + " from your total. Your total is $" + runningTotal);


There error I'm getting is that it says it cannot convert from null to double. I've never really used null this way before so any insight would be appreciated.

If there is another way to do it...don't be afraid to let me know. I've googled and can't come up with a way to do it.

TIA
 
Either use boolean literal/variable or use String
You cannot assign null to double.
(You can assign null to double wrapper class but wrapper class is for other purpose. I recommend you use boolean)
Code:
boolean indicatorToNull = false;
String indicatorToNull2 = null;
 
But if itemPrice == 0, what is there to back out?

_________________
Bob Rashkin
 
I don't get why you need the null value, maybe a -1 could do the job.

Cheers,
Dian
 
The whole code runs like this:

User inputs price. price gets added to a total.
Say user inputs a price that he doesn't want. He hits 0 to remove the last value he inputted. It removes the last value he entered from the running total, and continues on. When he's done, he enters -1 and then tax and change all gets calculated.

I've tried setting up two variables but it just isn't happening, I think I need to change my loop structure.
 
You definitely need to remember both the current value and the previously entered value. Then if current value == 0, then running total -= previous value.

What would be the correct action on two consecutive 0 entries?

_________________
Bob Rashkin
 
You slap the user for putting the wrong price in twice.
 
I guess this is some kind of asignment. If not, it makes no sense at all for me.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top