# Thread: I think I need a variable

1. Member
Join Date
Oct 2012
Location
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?

2. ## 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

3. Senior Member
Join Date
Aug 2009
Posts
294
Rep Power
0

## Re: I think I need a variable

Java 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..

4. ## Re: I think I need a variable

Java 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

5. Member
Join Date
Oct 2012
Location
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 :).

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•