wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Variable to be initialized Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Variable to be initialized Problem" Watch "Variable to be initialized Problem" New topic
Author

Variable to be initialized Problem

Shaktar Donetsk
Greenhorn

Joined: Nov 27, 2012
Posts: 7
Below is some code for a self checkout machine. My runningtotal variable is not adding up all the numbers the user inputs. It's only outputting the most recent user input. Any help would be appreciated

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


see embedded hint....

Shaktar Donetsk wrote:Below is some code for a self checkout machine. My runningtotal variable is not adding up all the numbers the user inputs. It's only outputting the most recent user input. Any help would be appreciated



Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Suggest you to go through Java(TM) Operators and find out about the error. Its pretty straight forward.
Shaktar Donetsk
Greenhorn

Joined: Nov 27, 2012
Posts: 7
Well I'm only a beginner. Is there a simple way and if so could you briefly explain it? Thanks
EDIT: This is only for a small assignment. My question is, how do I make it so runningtotal outputs the total instead of the price.
Here's a picture, it says the total is the inputted number when it should be an addition of all the prices that came before it.


[Thumbnail for variable.png]

Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Too slow in posting my reply!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Shaktar Donetsk wrote:Well I'm only a beginner. Is there a simple way and if so could you briefly explain it? Thanks


perhaps, this...



should be written better as this...




Does that help yet?

Henry
Shaktar Donetsk
Greenhorn

Joined: Nov 27, 2012
Posts: 7
No it's still not adding. I've been trying different things for hours, this is really wierd
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Shaktar Donetsk wrote:No it's still not adding. I've been trying different things for hours, this is really wierd


The unary plus operator doesn't really do much with primatives, so "+price" is basically the same as "price",

and...



is the same as ...



Or in other words, the runningtot is taking the value of price, because you are assigning the value of price to the runningtot variable.

Henry
Shaktar Donetsk
Greenhorn

Joined: Nov 27, 2012
Posts: 7
Oh. Well then how would I go about adding a users input with their previous one. Like a running total of their inputs?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Shaktar Donetsk wrote:Oh. Well then how would I go about adding a users input with their previous one. Like a running total of their inputs?


Well, since you have been doing this for hours, what have you tried so far?

Henry
Shaktar Donetsk
Greenhorn

Joined: Nov 27, 2012
Posts: 7
I created a topic on another site. I've only tried the for loop and a lot of other smaller things like moving the location of 'double runningtot' because it wasn't declared locally or something along those lines. Just tinkering with things but no clear answer.
Shaktar Donetsk
Greenhorn

Joined: Nov 27, 2012
Posts: 7
Any suggestions or fixes would be greatly appreciated.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Shaktar Donetsk wrote:I created a topic on another site. I've only tried the for loop and a lot of other smaller things like moving the location of 'double runningtot' because it wasn't declared locally or something along those lines. Just tinkering with things but no clear answer.


Yeah, "tinkering with things" with no clear direction is not very efficient. You should have at least have an idea of what you are tinkering with. I recommend taking a look at the Oracle tutorial, here is the pages for the operations and expressions.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html

perhaps it is a good place to start.

Henry
Shaktar Donetsk
Greenhorn

Joined: Nov 27, 2012
Posts: 7
I've got a project due soon though. I knew how to do it before but its slipped my mind. I know it's something very simple, can you show me?
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Shaktar Donetsk wrote:I've got a project due soon though. I knew how to do it before but its slipped my mind. I know it's something very simple, can you show me?


That wouldn't help you understand anything and after some time you will again face the same problem. Atleast go through the operator list through Henry's link and see whether there indeed is an operator like =+
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11411
    
  16

Shaktar Donetsk wrote:I've got a project due soon though.
That's really not relevant to us. We're happy to help, but if you make it URGENT or stressful, folks will just walk away and not bother.

Shaktar Donetsk wrote: I knew how to do it before but its slipped my mind. I know it's something very simple, can you show me?

The link Henry provided tells you what valid operators are. look at it CAREFULLY, and see if you can find "=+" (or perhaps something that looks a lot like it).


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Variable to be initialized Problem