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;```
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. ## 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.

3. ## Re: Smallest numbers

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 :)

4. ## Re: Smallest numbers

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

5. ## Re: Smallest numbers

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:
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. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## Re: Smallest numbers

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

7. ## Re: Smallest numbers

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.

8. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
786
Rep Power
5

## Re: Smallest numbers

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).

9. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## Re: Smallest numbers

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.

10. ## Re: Smallest numbers

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:
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. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## 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. ## Re: Smallest numbers

Originally Posted by Tolls
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

13. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## 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. ## Re: Smallest numbers

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 ;-)

15. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## Re: Smallest numbers

Validate the creation of a Player...:)

16. ## Re: Smallest numbers

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

17. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

Caveman...

18. ## Re: Smallest numbers

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!* ;-)

19. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## Re: Smallest numbers

Leave my willy-nilly out of this!
:)

20. ## Re: Smallest numbers

Originally Posted by Tolls
Caveman...
He's not a caveman, he's dutch... wooden shoes, wooden head, wouldn't listen...

Page 1 of 2 12 Last

#### Posting Permissions

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