Results 1 to 5 of 5
Like Tree3Likes
  • 3 Post By JosAH

Thread: I think I need a variable

  1. #1
    CannonVulcan is offline Member
    Join Date
    Oct 2012
    Location
    Trinidad
    Posts
    4
    Rep Power
    0

    Question 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. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default 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. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: I think I need a variable

    Quote Originally Posted by Addez View Post
    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
    DarrylBurke, Toll and liluma like this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    CannonVulcan is offline Member
    Join Date
    Oct 2012
    Location
    Trinidad
    Posts
    4
    Rep Power
    0

    Default 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

  1. How to compare variable with multiple variable?
    By mharckz009 in forum New To Java
    Replies: 5
    Last Post: 09-11-2012, 04:33 PM
  2. Replies: 1
    Last Post: 04-21-2012, 06:03 PM
  3. Dynamic variable name based on other variable
    By nadissen in forum Eclipse
    Replies: 4
    Last Post: 05-06-2011, 06:22 PM
  4. Replies: 11
    Last Post: 07-07-2010, 06:02 AM
  5. Variable name determined by another variable's value
    By Lumpkabob in forum New To Java
    Replies: 5
    Last Post: 04-14-2009, 08:00 AM

Posting Permissions

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