# I think I need a variable

• 10-25-2012, 03:00 PM
CannonVulcan
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, 03:11 PM
JosAH
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,

Jos
• 10-25-2012, 03:13 PM
Re: I think I need a variable
Code:

```String value = "1100g"; String gram = value.subString(value.length()-4,value.length()-1); String kilo = value.subString(0,value.length()-4);```
Something like that..
• 10-25-2012, 03:59 PM
JosAH
Re: I think I need a variable
Quote:

Originally Posted by Addez
Code:

```String value = "1100g"; String gram = value.subString(value.length()-4,value.length()-1); String kilo = value.subString(0,value.length()-4);```
Something like that..

Cool, try that with "42g".

kind regards,

Jos
• 10-26-2012, 12:46 PM
CannonVulcan
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 :).