Java - Comparator, sorting arrayLists
Hi
So as you might guessed i am trying to sort my arrayList of scores.
I have been instructed to look at the comparator and compareTo().
And i have really been trying, i tried to look at examples, guides and the API.
But it just wont stick, i just cant figure out where to put the compareTo and how to implement it.
Is there any kind soul out there who could try to point/show/tell how i can get this to work?
Best regards
//Martin
Code:
import java.util.*;
public class Game {
public static void main(String[] args) {
/*ArrayList<Score> scores = new ArrayList<Score>();
Scanner input = new Scanner(System.in);*/
{
guessNumber newGame = new guessNumber();
newGame.Start();
}
}
}
Code:
import java.util.*;
public class Score {
int theScore = 0;
double theTime = 0;
String playerName;
public Score (int theScore, double theTime, String playerName){
this.theScore = theScore;
this.theTime = theTime;
this.playerName = playerName;
}
public String toString(){
String highScorelist = (playerName + "\t" + " " + theScore + "\t" +" " + ((int)theTime/1000));
return highScorelist;
}
}
Code:
import java.util.*;
import java.io.*;
public class guessNumber {
public void Start() {
Random random = new Random();
boolean playAgain = true;
String answer = "Yes";
String quit = "Quit";
ArrayList<Score> scores = new ArrayList<Score>();
while (playAgain == true)
{
System.out.println("\nWelcome to Guess the Number!!");
String name;
int guess = -1;
int tries = 0;
int number = random.nextInt(1000) + 1;
System.out.println(number);
Scanner input = new Scanner(System.in);
System.out.println("\nPlease guess a number between 0 and 1000: ");
while (guess != number){
long startTime = System.currentTimeMillis();
String guess1 = input.next();
if (guess1.equalsIgnoreCase(quit))
{
System.out.println("Thanks for playing!");
System.exit(0);
}
try {
guess = Integer.parseInt(guess1);
} catch (NumberFormatException nfe) {
System.out.println("Please enter a \"Number\" between 0 and 1000: ");
}
if (guess > number && guess < 1001) {
System.out.println("Your guess is too high, try with a lower number: ");
tries++;
}
else if (guess < number && guess >= 0) {
System.out.println("Your guess is too low, try with a higher number: ");
tries++;
}
if (guess <-1 || guess > 1001) {
System.out.println("Try between 0 and 1000 instead: ");
}
if (guess == number) {
System.out.println("\nCorrect!");
tries++;
long endTime = System.currentTimeMillis();
long gameTime = endTime - startTime;
System.out.println("\nYou guess the correct answer in " + tries + " tries and " + (gameTime/1000) + " seconds" );
System.out.println("\nEnter your name: ");
name = input.next();
Score currentScore = new Score(tries, gameTime, name);
scores.add(currentScore);
System.out.print("\nPlay again? (Yes or No)" );
name = input.next();
if (answer.equalsIgnoreCase("Yes")) {
System.out.println("\nHigh Score\nName \t Guess \t Time");
for (int i = 0;i < scores.size(); i++)
{
System.out.println(scores.get(i));
playAgain = true;
}
}
else if (answer.equalsIgnoreCase("N")) {
playAgain = false;
}
else {
playAgain = false;
}
if (playAgain == false) {
System.out.println("\nThank you for playing!");
}
}
}
}
}
}