Page 1 of 2 12 LastLast
Results 1 to 20 of 26
Like Tree1Likes

Thread: Smallest numbers

  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Smallest numbers

    I'm confused.
    I have those ints:
    Java Code:
        int person1rank = 27;
        int person1level = 46;
    
        int person2rank = 76;
        int person2level = 47;
    
        int person3rank = 85;
        int person3level = 46;
    
        int person4rank = 103;
        int person4level = 42;
    
        int person5rank = 107;
        int person5level = 48;
    I have this method to see which is the smallest level:
    Java Code:
    public void getLowestOpponent() {
            if (person1level < person2level &&
                    person1level < person3level &&
                    person1level < person4level &&
                    person1level < person5level) {
                System.out.println("lowest 1");
            } else if (person2level < person1level &&
                    person2level < person3level &&
                    person2level < person4level &&
                    person2level < person5level) {
                System.out.println("lowest 2");
            } else if (person3level < person1level &&
                    person3level < person2level &&
                    person3level < person4level &&
                    person3level < person5level) {
                System.out.println("lowest 3");
            } else if (person4level < person1level &&
                    person4level < person2level &&
                    person4level < person3level &&
                    person4level < person5level) {
                System.out.println("lowest 4");
            } else if (person5level < person1level &&
                    person5level < person2level &&
                    person5level < person3level &&
                    person5level < person4level) {
                System.out.println("lowest 5");
            } else {
    
                System.out.println("There are numbers which are the same");
    
            }
        }
    How can I make it so for example if the output is "There are numbers which are the same" it would check the rank of those numbers and see which person has the smallest rank ?

    By the way: the ints will always change.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Smallest numbers

    I'd do things totally differently as I would make a Person class and give it two int fields, rank and level. I would make Person implement Comparable<Person> and give it a compareTo(Person otherPerson) method, that would do just what you're asking -- sort first on level and then if == sort on rank.
    sunde887 likes this.

  3. #3
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Smallest numbers

    Quote Originally Posted by Fubarable View Post
    I'd do things totally differently as I would make a Person class and give it two int fields, rank and level. I would make Person implement Comparable<Person> and give it a compareTo(Person otherPerson) method, that would do just what you're asking -- sort first on level and then if == sort on rank.
    Good idea :P
    I will try it
    Thank you :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Smallest numbers

    Quote Originally Posted by PhQ View Post
    Good idea :P
    I will try it
    Thank you :)
    Good luck, and please let us know how things turn out!

  5. #5
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Smallest numbers

    Quote Originally Posted by Fubarable View Post
    Good luck, and please let us know how things turn out!
    I have done it the way you told me and it works perfectly.
    But I think that the way I did it was the long way to do it
    Here is my compareTo method:
    Java Code:
    public int compareTo(Player otherPerson) {
            if(otherPerson.level == level){
                  if(otherPerson.rank < rank){
                      return 1;
                  }
            }else{
                if(otherPerson.level > level){
                    return 1;
                }
            }
            return 0;
        }
    and here is my method of finding the lowest opponent:
    Java Code:
    public int getLowestOpponent() {
    
            player1 = new Player(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 1), WebMethods.getRankItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 1));
            player2 = new Player(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 2), WebMethods.getRankItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 2));
            player3 = new Player(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 3), WebMethods.getRankItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 3));
            player4 = new Player(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 4), WebMethods.getRankItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 4));
            player5 = new Player(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 5), WebMethods.getRankItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 5));
    
            if (player1.compareTo(player2) == 1 &&
                    player1.compareTo(player3) == 1 &&
                    player1.compareTo(player4) == 1 &&
                    player1.compareTo(player5) == 1 &&
                    !getLostNames().contains(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 1))) {
                return 1;
            } else if (player2.compareTo(player1) == 1 &&
                    player2.compareTo(player3) == 1 &&
                    player2.compareTo(player4) == 1 &&
                    player2.compareTo(player5) == 1 &&
                    !getLostNames().contains(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 2))) {
                return 2;
            } else if (player3.compareTo(player1) == 1 &&
                    player3.compareTo(player2) == 1 &&
                    player3.compareTo(player4) == 1 &&
                    player3.compareTo(player5) == 1 &&
                    !getLostNames().contains(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 3))) {
                return 3;
            } else if (player4.compareTo(player1) == 1 &&
                    player4.compareTo(player2) == 1 &&
                    player4.compareTo(player3) == 1 &&
                    player4.compareTo(player5) == 1 &&
                    !getLostNames().contains(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 4))) {
                return 4;
            } else if (player5.compareTo(player1) == 1 &&
                    player5.compareTo(player2) == 1 &&
                    player5.compareTo(player3) == 1 &&
                    player5.compareTo(player4) == 1 &&
                    !getLostNames().contains(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 5))) {
                return 5;
    
            } else {
                System.out.println("No good racers found. Choosing the first one.");
                return 1;
            }
        }
    Is there a quicker way to get the LowestOpponent?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Smallest numbers

    Stick your Player objects in a List and use Arrays.sort().
    (I think that's the method).

  7. #7
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Smallest numbers

    Quote Originally Posted by Tolls View Post
    Stick your Player objects in a List and use Arrays.sort().
    (I think that's the method).
    But then they will all get mixed up and I will not know which one is which.

  8. #8
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Smallest numbers

    Quote Originally Posted by PhQ View Post
    Is there a quicker way to get the LowestOpponent?
    You could add all Players in a collection and then you could use Collections.min(your players collection).

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Smallest numbers

    Quote Originally Posted by PhQ View Post
    But then they will all get mixed up and I will not know which one is which.
    Give them an id attribute.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Smallest numbers

    Quote Originally Posted by PhQ View Post
    I have done it the way you told me and it works perfectly.
    But I think that the way I did it was the long way to do it
    Here is my compareTo method:
    Java Code:
    public int compareTo(Player otherPerson) {
            if(otherPerson.level == level){
                  if(otherPerson.rank < rank){
                      return 1;
                  }
            }else{
                if(otherPerson.level > level){
                    return 1;
                }
            }
            return 0;
        }
    Sorry, but it looks incomplete to me as you need to check for all 5 possibilities, . I'd do something more along the lines of,

    Java Code:
       public int compareTo(Player otherPlayer) {
          // first check all possibilities of level 
          // if that's the order of compare that you desire
          if (level > otherPlayer.level) {
             return 1;
          } else if (level < otherPlayer.level) {
             return -1;
          } 
          
          // levels are ==, must check ranks.
          else if (rank > otherPlayer.rank) {
             return 1;
          } else if (rank < otherPlayer.rank) {
             return -1;
          }
    
          // everything's equal, return 0
          return 0;
       }

    and here is my method of finding the lowest opponent:
    Java Code:
    public int getLowestOpponent() {
    
            player1 = new Player(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 1), WebMethods.getRankItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 1));
            player2 = new Player(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 2), WebMethods.getRankItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 2));
            player3 = new Player(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 3), WebMethods.getRankItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 3));
            player4 = new Player(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 4), WebMethods.getRankItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 4));
            player5 = new Player(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 5), WebMethods.getRankItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 5));
    
            if (player1.compareTo(player2) == 1 &&
                    player1.compareTo(player3) == 1 &&
                    player1.compareTo(player4) == 1 &&
                    player1.compareTo(player5) == 1 &&
                    !getLostNames().contains(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 1))) {
                return 1;
            } else if (player2.compareTo(player1) == 1 &&
                    player2.compareTo(player3) == 1 &&
                    player2.compareTo(player4) == 1 &&
                    player2.compareTo(player5) == 1 &&
                    !getLostNames().contains(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 2))) {
                return 2;
            } else if (player3.compareTo(player1) == 1 &&
                    player3.compareTo(player2) == 1 &&
                    player3.compareTo(player4) == 1 &&
                    player3.compareTo(player5) == 1 &&
                    !getLostNames().contains(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 3))) {
                return 3;
            } else if (player4.compareTo(player1) == 1 &&
                    player4.compareTo(player2) == 1 &&
                    player4.compareTo(player3) == 1 &&
                    player4.compareTo(player5) == 1 &&
                    !getLostNames().contains(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 4))) {
                return 4;
            } else if (player5.compareTo(player1) == 1 &&
                    player5.compareTo(player2) == 1 &&
                    player5.compareTo(player3) == 1 &&
                    player5.compareTo(player4) == 1 &&
                    !getLostNames().contains(WebMethods.getLevelItem(WebMethods.getRaceTableText(BotConstants.DivNodeNames.CLASS, "tbrace oponentlist"), 5))) {
                return 5;
    
            } else {
                System.out.println("No good racers found. Choosing the first one.");
                return 1;
            }
        }
    Is there a quicker way to get the LowestOpponent?
    Gack, that's hard-coded nightmare. I second all the other recommendations for using Collections.sort(...). Give your Player objects name and/or id fields, but sort them cleanly and flexibly, not with hard-coded rigidity.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Smallest numbers

    Java Code:
       public int compareTo(Player otherPlayer) {
            // if level is the same then return the difference between ranks.
            if (level == otherPlayer.level) {
                return rank - otherPlayer.rank;
            } else {
                return level - otherPlayer.level;
            }
       }
    :)

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Smallest numbers

    Quote Originally Posted by Tolls View Post
    Java Code:
       public int compareTo(Player otherPlayer) {
            // if level is the same then return the difference between ranks.
            if (level == otherPlayer.level) {
                return rank - otherPlayer.rank;
            } else {
                return level - otherPlayer.level;
            }
       }
    :)
    If you use (a-b) for the comparison result, it fails horrible if (a-b) < Integer.MIN_VALUE ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Smallest numbers

    Bugger.
    I knew I should have checked it.

    However, in my defence, the data appears to be all positive...;)
    I'm relying on the OP to ensure the data in Player is valid...:p

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Smallest numbers

    Quote Originally Posted by Tolls View Post
    Bugger.
    I knew I should have checked it.

    However, in my defence, the data appears to be all positive...;)
    I'm relying on the OP to ensure the data in Player is valid...:p
    Sure, just document that silly behaviour and then all of a sudden it's a feature!

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Smallest numbers

    Validate the creation of a Player...:)

    ValidationExceptions are your friend.

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Smallest numbers

    Quote Originally Posted by Tolls View Post
    Validate the creation of a Player...:)

    ValidationExceptions are your friend.
    Sure, drag in that xml crap (if you want your code size to explode and slow down the performance of your code to below zero that is ;-)
    Why not add an html interface as well? And a database interface?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Smallest numbers

    Caveman...

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Smallest numbers

    Quote Originally Posted by Tolls View Post
    Caveman...
    Yes, us Neanderthal are able to program with just a handful of pebbles; you twentyfirst century degenerates need xml and xml configuration files, html interfaces and all that hanky-panky before you can even start your willy-nilly programs ;-)

    kindest regards,

    *grunt!* ;-)
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Smallest numbers

    Leave my willy-nilly out of this!
    :)

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 12
    Last Post: 09-08-2011, 06:03 PM
  2. Return smallest value
    By jaylimix in forum New To Java
    Replies: 8
    Last Post: 06-06-2011, 07:11 PM
  3. Replies: 11
    Last Post: 01-14-2011, 07:36 PM
  4. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-15-2008, 12:46 AM
  5. Getting smallest possible Double value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-06-2007, 03:15 PM

Posting Permissions

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