I need to read players from text file, and then output top 3, 5 or 10 players depends on users choice. Format of data in text file is:
- Name, date, correct answerws, points
- John, 21.8.2016, 4/5, 80
- Edy, 21.8.2016, 5/5, 100
I need to sort them by points and then output best 3,5 or 10 players as i already write.
Here is what i done so far:
public static void topPlayers(){
File f=new File("results.txt");
Scanner scf=new Scanner(f);
Scanner sc=new Scanner(System.in);
Scanner sc2=new Scanner(f);
while(sc2.hasNextLine()){
String p1=scf.nextLine();
String[] niz=p1.split(", ");
}
sc2.close();
System.out.println("Choose an option: ");
System.out.println("1. Top 3 players");
System.out.println("2. Top 5 players");
System.out.println("3. Top 10 players");
int op=sc.nextInt();
if(op==1){
System.out.println("Top 3 players: ");
for(int i=0; i<3; i++){
//System.out.println(....);
}
}
else if(op==2){
System.out.println("Top 5 players: ");
for(int i=0; i<5; i++){
//System.out.println(....);
}
}
else if(op==3){
System.out.println("Top 10 players: ");
for(int i=0; i<10; i++){
//System.out.println(....);
}
}
else{
System.out.println("Wrong option!");
}
}
How to sort this lines from text file by players point?