Good Evening
I have been stuck for a while now while trying to figure out how i can keep my ArrayList sorted when it receives new input.
I tried the collections.sort(scores) but i get a error saying
"Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (ArrayList<Score>). The inferred type Score is not a
valid substitute for the bounded parameter <T extends Comparable<? super T>>"
What i now understand is that i need to have a Comparator?
I have been studying that one for the past 2 hour but i really cant figure it out.
Maybe i am to far ahead from where the teacher wants us to be, but this is the game i built and that i find works decently well.
So if some kind soul out there could try to explain it better then the examples out there i would be super happy. Or break it up so even this 1 month old programmer could work with it.
Best Regards
//Martin
import java.util.*;
public class Start {
public static void main(String[] args) {
ArrayList<Score> scores = new ArrayList<Score>();
Scanner input = new Scanner(System.in);
{
Game2 newGame = new Game2();
newGame.Start();
}
}
}
import java.util.*;
import java.io.*;
public class Game2 {
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("\n\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("\nGuess a number between 0 and 1000");
long startTime = System.currentTimeMillis();
while (guess != number){
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("Try a number between 0 and 1000 instead.");
}
if (guess > number && guess < 1001) {
System.out.println("Your guess is too high");
tries++;
}
else if (guess < number && guess >= 0) {
System.out.println("Your guess is too low");
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("\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!");
}
}
}
}
}
}
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;
}
}