Get highest Int from config

Discussion in 'Plugin Development' started by JordyPwner, Dec 24, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    JordyPwner

    Im trying to get the highest int from a config but idk. I cant find any good example ;3

    this is how my config looks like:

    JordyPwner:
    'UUID: my UUID is here :p':
    Coins: 15
    scorpian442002:
    'UUID: his UUID here':
    Coins: 50
     
  2. Online

    timtower Administrator Administrator Moderator

    @JordyPwner You need to loop through the keys, check if their value is higher then your current value.
     
  3. Offline

    JordyPwner

    And how would i do that?
     
  4. Online

    timtower Administrator Administrator Moderator

    Get the keys. Initialize a value to -5000 or something along those lines.
    the loop through the keys, get the value, if the value is bigger then the initialized one set the top to the new key
     
  5. Offline

    mine-care

    @JordyPwner
    have a for loop loop through all the values of coins, while looping have a integer (out of the loop) that is equal to 0, in your loop check if the curent value of the coins is > than that int, if it is, make the int equal to it, as the loop goes on, it will do the ckecks and at the end you will have the max value kept in that int :)
    EDIT ninja'ed by @timtower
     
  6. Offline

    Unica

    What
    @mine-care said :)

    Would be something like his
    Code:
    int highest = 0;
    for(loop){ //x is the coins
         if(x > highest) highest = x ;
    }
     
  7. Offline

    mine-care

    @Unica thought i explained it well :3
    Thanks =)
     
  8. Offline

    JordyPwner

    Last edited: Dec 24, 2014
  9. Offline

    mine-care

    @JordyPwner we thought you wanted to display the one with the highest score. Now the thing changes.
    You can turn those values to a array, so loop throyght them and put them in a awway and then you can use Arrays.sort(Array);
    so a basic example
    Code:
    int[]  coins = {9,423,1,886,23057,2,54,30,97};
    Arrays.sort(coins);
     
  10. Offline

    JordyPwner

    Can you show me example? Sorry i never did this before
     
  11. Offline

    bennie3211

    @JordyPwner If you didn't do this before, you will need to learn the basics (of Arrays) first :s I'm sorry to say it, but it will help you with this kind of questions ;)

    But the Arrays.sort(); will sort the array for you. So you will load all current values in, then sort them, and loop through the sorted array to get the highest X coins.
     
  12. Offline

    JordyPwner

    Im asking for a example -_-
     
  13. Offline

    bennie3211

    @JordyPwner no one will give you the exact code, no one will spoonfeed you. The answer is given you a couple of times... @Unica @mine-care and @timtower showed you how to do it. And now I will give you an psuedo code step by step example too.

    1.) Load all values to an array
    2.) Use Arrays.sort(<Your array name>); to sort the array from low to high
    3.) Get the first result of that array

    Thats what is told to you before, not that hard.
     
    Cycryl likes this.
  14. Online

    timtower Administrator Administrator Moderator

    @bennie3211 I am pretty sure that he also wants the key though.
     
  15. Offline

    Experminator

    Code:
    Map<UUID, Integer> coins = new HashMap<UUID, Integer>();
    
    public int getPlayerWithMostCoins(UUID uuid){
       for(int x =  coins.get(uuid); x > coins.get(uuid); x++){
             List<Integer> sorted = Arrays.sort(x);
             for(Integer coin : sorted){
                   return coin;
            }
      } 
      return 0;
    }
     
  16. Offline

    JordyPwner

    but im storing it in a config ;p3
     
  17. Offline

    mine-care

    @JordyPwner ehh modify it to get from config! Read Yaml configuration api's docs and you will find how
     
  18. Offline

    Shawckz

    public String getPlayerWithMostCoins(HashMap<String, Integer> key){

    int x = 0;
    String p;
    for(String s : key.keySet()){
    if(key.get(s) > x)
    x = key.get(s);
    p = s;
    }
    return s;
    }

    wrote w/o ide forgive any errors
     
Thread Status:
Not open for further replies.

Share This Page