Solved How do i merge 2 Ints?

Discussion in 'Spigot Plugin Development' started by redstonebiten, May 13, 2016.

  1. Question in title..

    Code (Text):
    player.sendMessage("" + config.getInt("RedPercentage") + config.getInt("GreenPercentage"));
    Code (Text):
    RedPercentage: 49
    GreenPercentage: 2
    I get 492 instead of 51.. why?
  2. MiniDigger


    thats easy to solve:
    "" is a string. you add 49 to a string and get "49" you add 2 to that and get "492".
    what you want is to add 49 and 2 and then make a string out out that. so you can just add some brackets around your int addition so that it gets executed first
  3. Yeah i did that... :p i was just gonna make the post solved xD
  4. Oh... wait..
    So i can't do it like this?
    Code (Text):
            int red = config.getInt("RedPercentage");
            int green = config.getInt("GreenPercentage");
    player.sendmessage("" + red + green);
    That did not work but this worked:
    Code (Text):
            int red = config.getInt("RedPercentage") + config.getInt("GreenPercentage");
  5. In string you are appending objects to the preview part of the string, you cannot add an int specifically from a string.
  6. MiniDigger


    not that
    but do that player.sendmessage("" + (red + green)); or that player.sendmessage(red + green + "");
  7. Forgot about that :p I've been taking a break off java for a bit, and using c#, the naming conventions with it suggest method/variables to start with upper case(disgusting, I still use java conventions :p) because c# is like java, it's not to hard to switch between the two for a bit)
  8. already got it fixed :p
  9. (Maths)

    new String("i love cake as i ate " + 200 + 200);
    If outputted: i love cake as i ate 200200

    new String("i love cake as i ate " + (200 + 200));
    If outputted: i love cake as i ate 400