# Smallest numbers

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-18-2011, 10:55 PM
PhQ
Smallest numbers
I'm confused.
I have those ints:
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:
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.
• 09-19-2011, 12:30 AM
Fubarable
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.
• 09-19-2011, 12:47 AM
PhQ
Re: Smallest numbers
Quote:

Originally Posted by Fubarable
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 :)
• 09-19-2011, 12:48 AM
Fubarable
Re: Smallest numbers
Quote:

Originally Posted by PhQ
Good idea :P
I will try it
Thank you :)

Good luck, and please let us know how things turn out!
• 09-19-2011, 05:58 PM
PhQ
Re: Smallest numbers
Quote:

Originally Posted by Fubarable
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:
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:
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?
• 09-19-2011, 06:11 PM
Tolls
Re: Smallest numbers
Stick your Player objects in a List and use Arrays.sort().
(I think that's the method).
• 09-19-2011, 06:18 PM
PhQ
Re: Smallest numbers
Quote:

Originally Posted by Tolls
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.
• 09-19-2011, 06:19 PM
eRaaaa
Re: Smallest numbers
Quote:

Originally Posted by PhQ
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).
• 09-19-2011, 06:28 PM
Tolls
Re: Smallest numbers
Quote:

Originally Posted by PhQ
But then they will all get mixed up and I will not know which one is which.

Give them an id attribute.
• 09-19-2011, 10:39 PM
Fubarable
Re: Smallest numbers
Quote:

Originally Posted by PhQ
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:
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,

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;   }```

Quote:

and here is my method of finding the lowest opponent:
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.
• 09-20-2011, 09:38 AM
Tolls
Re: Smallest numbers
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;         }   }```
:)
• 09-20-2011, 09:46 AM
JosAH
Re: Smallest numbers
Quote:

Originally Posted by Tolls
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
• 09-20-2011, 10:01 AM
Tolls
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
• 09-20-2011, 10:21 AM
JosAH
Re: Smallest numbers
Quote:

Originally Posted by Tolls
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 ;-)
• 09-20-2011, 10:32 AM
Tolls
Re: Smallest numbers
Validate the creation of a Player...:)

• 09-20-2011, 11:32 AM
JosAH
Re: Smallest numbers
Quote:

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

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
• 09-20-2011, 11:59 AM
Tolls
Re: Smallest numbers
Caveman...
• 09-20-2011, 12:25 PM
JosAH
Re: Smallest numbers
Quote:

Originally Posted by Tolls
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!* ;-)
• 09-20-2011, 12:28 PM
Tolls
Re: Smallest numbers
Leave my willy-nilly out of this!
:)
• 09-20-2011, 01:22 PM
Fubarable
Re: Smallest numbers
Quote:

Originally Posted by Tolls
Caveman...

He's not a caveman, he's dutch... wooden shoes, wooden head, wouldn't listen...
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last