Results 1 to 5 of 5
Thread: I think I need a variable
- 10-25-2012, 02:00 PM #1
Member
- Join Date
- Oct 2012
- Location
- Trinidad
- Posts
- 4
- Rep Power
- 0
I think I need a variable
Firstly my name is Mark I'm new to these forums and I am currently doing java programming in college. I am using Netbeans IDE for program development.
I need to create a program that adds two weights in kg and g and add them together. for example if a user entered 1kg and 600g as the first number and 3kg and 500g as the second number my answer needs to show 5kg 100g.
Now I know how to do the majority of the program my problem is how do I show kg and g? do I need a variable that says 1000g = 1 kg so that if I have 1100g it would automatically say 1kg and 100g? if so how do I create that variable? and if not what is it that I have to do?
- 10-25-2012, 02:11 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Re: I think I need a variable
Do all your calculations in grams, next have a look at the integer division operator / and the modulo operator %.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-25-2012, 02:13 PM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
Re: I think I need a variable
Something like that..Java Code:String value = "1100g"; String gram = value.subString(value.length()-4,value.length()-1); String kilo = value.subString(0,value.length()-4);
- 10-25-2012, 02:59 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 10-26-2012, 11:46 AM #5
Member
- Join Date
- Oct 2012
- Location
- Trinidad
- Posts
- 4
- Rep Power
- 0
Re: I think I need a variable
Thanks guys I didnt quite understand what Addez posted still very much a beginner. But it looked like the string = 1100g was a fixed value. I did follow JosAH's advice and used modulus to get the grams and just subtracted modulus from total to get the kg thanks for help :).
Similar Threads
-
How to compare variable with multiple variable?
By mharckz009 in forum New To JavaReplies: 5Last Post: 09-11-2012, 04:33 PM -
Creating an instance variable in one class that connects to another instance variable
By SpicyElectricity in forum New To JavaReplies: 1Last Post: 04-21-2012, 06:03 PM -
Dynamic variable name based on other variable
By nadissen in forum EclipseReplies: 4Last Post: 05-06-2011, 06:22 PM -
How do I substitute any variable for a hardcoded variable
By Weazel Boy in forum New To JavaReplies: 11Last Post: 07-07-2010, 06:02 AM -
Variable name determined by another variable's value
By Lumpkabob in forum New To JavaReplies: 5Last Post: 04-14-2009, 08:00 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks