Results 1 to 20 of 26
Thread: Smallest numbers
 09182011, 10:55 PM #1
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;
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"); } }
By the way: the ints will always change.

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.
 09192011, 12:47 AM #3

 09192011, 05:58 PM #5
Re: Smallest numbers
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; }
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; } }
 09192011, 06:11 PM #6Moderator
 Join Date
 Apr 2009
 Posts
 12,371
 Rep Power
 21
Re: Smallest numbers
Stick your Player objects in a List and use Arrays.sort().
(I think that's the method).
 09192011, 06:18 PM #7
 09192011, 06:19 PM #8Senior Member
 Join Date
 Oct 2010
 Location
 Germany
 Posts
 787
 Rep Power
 6
 09192011, 06:28 PM #9Moderator
 Join Date
 Apr 2009
 Posts
 12,371
 Rep Power
 21

Re: Smallest numbers
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; } }
 09202011, 09:38 AM #11Moderator
 Join Date
 Apr 2009
 Posts
 12,371
 Rep Power
 21
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; } }
 09202011, 09:46 AM #12
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,961
 Blog Entries
 7
 Rep Power
 22
 09202011, 10:01 AM #13Moderator
 Join Date
 Apr 2009
 Posts
 12,371
 Rep Power
 21
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
 09202011, 10:21 AM #14
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,961
 Blog Entries
 7
 Rep Power
 22
 09202011, 10:32 AM #15Moderator
 Join Date
 Apr 2009
 Posts
 12,371
 Rep Power
 21
Re: Smallest numbers
Validate the creation of a Player...:)
ValidationExceptions are your friend.
 09202011, 11:32 AM #16
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,961
 Blog Entries
 7
 Rep Power
 22
Re: Smallest numbers
I have the stamina of a seal; I lie on the beach instead of running on it.
 09202011, 11:59 AM #17Moderator
 Join Date
 Apr 2009
 Posts
 12,371
 Rep Power
 21
Re: Smallest numbers
Caveman...
 09202011, 12:25 PM #18
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,961
 Blog Entries
 7
 Rep Power
 22
Re: Smallest numbers
I have the stamina of a seal; I lie on the beach instead of running on it.
 09202011, 12:28 PM #19Moderator
 Join Date
 Apr 2009
 Posts
 12,371
 Rep Power
 21
Re: Smallest numbers
Leave my willynilly out of this!
:)

Re: Smallest numbers
Similar Threads

How to display largest and smallest numbers entered..?
By Art_88 in forum New To JavaReplies: 12Last Post: 09082011, 05:03 PM 
Return smallest value
By jaylimix in forum New To JavaReplies: 8Last Post: 06062011, 06:11 PM 
Problem getting numbers from user and finding smallest two numbers
By radhi16 in forum New To JavaReplies: 11Last Post: 01142011, 07:36 PM 
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10142008, 11:46 PM 
Getting smallest possible Double value
By Java Tip in forum Java TipReplies: 0Last Post: 12062007, 03:15 PM
Bookmarks