• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Variable to be initialized Problem

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too slow in posting my reply!
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any suggestions or fixes would be greatly appreciated.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 =+
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
I do some of my very best work in water. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic