Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variable to be initialized Problem

 
Shaktar Donetsk
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suggest you to go through Java(TM) Operators and find out about the error. Its pretty straight forward.
 
Shaktar Donetsk
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
variable.png
[Thumbnail for variable.png]
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too slow in posting my reply!
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it's still not adding. I've been trying different things for hours, this is really wierd
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any suggestions or fixes would be greatly appreciated.
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic