playerData2.txt Anthony 2416 Caleb 73 Christian 94 Daniel 3968 Elijah 3553 Gabri
ID: 3877357 • Letter: P
Question
playerData2.txt
Anthony 2416
Caleb 73
Christian 94
Daniel 3968
Elijah 3553
Gabriel 1870
James 2700
Jaylen 1440
Jordan 3583
Joshua 2388
Justin 3170
Malik 1946
Michael 1754
Tyler 92
Xavier 2584
Aaliyah 452
Alexis 2312
Angel 979
Brianna 2752
Destiny 4991
Gabrielle 2992
Hannah 4830
Isis 3258
Jasmine 4391
Jordan 2720
Kennedy 1760
Laila 4636
Makayla 535
Sydney 4348
Tiana 4347
Dalila 3372
Marlie 1577
Irene 1476
Demetra 2485
Gaynor 2460
Birgit 2387
Aubine 2344
Noel 490
Sarah 1920
Patrica 1541
Karee 1842
Cacilie 3847
Eimile 4974
Kaila 4823
Ileana 800
Olive 2903
Arlina 3427
Verile 1752
Eirena 1517
Lindie 1774
George 702
Cindie 1977
Persis 4611
Buffy 226
Priscilla 3276
Micheline 3032
Trudie 4256
Libbie 4090
Hermina 3559
Millicent 376
Candide 3442
Darline 2159
Clarine 859
Hetty 3205
Dorelia 15
Rosamund 2039
Doralia 4551
Dorisa 1085
Stephine 477
Conni 4696
Lucila 3028
Rubie 3594
Felicle 1075
Zola 265
Ilene 3005
Mildred 4972
Cinnamon 874
Mercy 4044
Bernardine 443
Carie 852
Explanation / Answer
Copy to code:
package normalClasses;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class PlayerStats {
public static void main(String[] args){
//create two array each of 100 size
String playerName[] = new String[100];
int score[] = new int[100];
//calling readfile function
int noOfPlayer=readFile(playerName, score);
System.out.println("--------------------Original record--------------------");
//printing details
printDetails(playerName,score,noOfPlayer);
//calling average function
double averageHighScore = calculateAverage(score,noOfPlayer);
System.out.println("Players average high score is :: "+averageHighScore);
//calling sort by score function
sortListByScore(playerName,score,noOfPlayer);
System.out.println("--------------------Record after sort by score--------------------");
printDetails(playerName,score,noOfPlayer); //printing details
//topx will print list of size topx
int topX =10;
printTopXPlayer(playerName,score,topX); //printing details
}
public static int readFile(String playersName[],int playerScore[]){
File file = new File("input.txt"); //reading data from this file
Scanner reader;
int playerIndex=-1; //keep track of number of records
try {
reader = new Scanner(file);
while(reader.hasNext()){
playerIndex++;
String line = reader.nextLine();
playersName[playerIndex] = line.split(" ")[0]; //spliting line by space,0 is player name
playerScore[playerIndex] = Integer.parseInt(line.split(" ")[1]); //spliting line by space,1 is score
}
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return playerIndex;
}
private static double calculateAverage(int[] score, int noOfPlayer) {
double totalScore = 0;
for(int i=0;i<=noOfPlayer;i++){
totalScore+=score[i];
}
return totalScore/noOfPlayer;
}
private static void sortListByScore(String[] playerName, int[] score, int noOfPlayer) {
for (int i = 0; i < noOfPlayer; i++)
{
int min_idx = i;
for (int j = i+1; j <= noOfPlayer; j++)
if (score[j] > score[min_idx])
min_idx = j;
int temp = score[i];
score[i] = score[min_idx];
score[min_idx] = temp;
String temp1 = playerName[i];
playerName[i] = playerName[min_idx];
playerName[min_idx] = temp1;
}
}
public static void printDetails(String[] playerName, int[] score, int playerCount){
System.out.printf("%-5s%s"," ","Players information"); //using printf for format output
System.out.printf(" %-20s%-20s","Player Name","High Score");
for(int i=0;i<=playerCount;i++){
System.out.printf(" %-20s%-20d",playerName[i],score[i]);
}
System.out.println();
}
private static void printTopXPlayer(String[] playerName, int[] score, int topX) {
System.out.println("--------------------Top 10 score players are--------------------");
printDetails(playerName,score,topX);
}
}
Output::
--------------------Original record--------------------
Players information
Player Name High Score
Anthony 2416
Caleb 73
Christian 94
Daniel 3968
Elijah 3553
Gabriel 1870
James 2700
Jaylen 1440
Jordan 3583
Joshua 2388
Justin 3170
Malik 1946
Michael 1754
Tyler 92
Xavier 2584
Aaliyah 452
Alexis 2312
Angel 979
Brianna 2752
Destiny 4991
Gabrielle 2992
Hannah 4830
Isis 3258
Jasmine 4391
Jordan 2720
Kennedy 1760
Laila 4636
Makayla 535
Sydney 4348
Tiana 4347
Dalila 3372
Marlie 1577
Irene 1476
Demetra 2485
Gaynor 2460
Birgit 2387
Aubine 2344
Noel 490
Sarah 1920
Patrica 1541
Karee 1842
Cacilie 3847
Eimile 4974
Kaila 4823
Ileana 800
Olive 2903
Arlina 3427
Verile 1752
Eirena 1517
Lindie 1774
George 702
Cindie 1977
Persis 4611
Buffy 226
Priscilla 3276
Micheline 3032
Trudie 4256
Libbie 4090
Hermina 3559
Millicent 376
Candide 3442
Darline 2159
Clarine 859
Hetty 3205
Dorelia 15
Rosamund 2039
Doralia 4551
Dorisa 1085
Stephine 477
Conni 4696
Lucila 3028
Rubie 3594
Felicle 1075
Zola 265
Ilene 3005
Mildred 4972
Cinnamon 874
Mercy 4044
Bernardine 443
Carie 852
Players average high score is :: 2473.7974683544303
--------------------Record after sort by score--------------------
Players information
Player Name High Score
Destiny 4991
Eimile 4974
Mildred 4972
Hannah 4830
Kaila 4823
Conni 4696
Laila 4636
Persis 4611
Doralia 4551
Jasmine 4391
Sydney 4348
Tiana 4347
Trudie 4256
Libbie 4090
Mercy 4044
Daniel 3968
Cacilie 3847
Rubie 3594
Jordan 3583
Hermina 3559
Elijah 3553
Candide 3442
Arlina 3427
Dalila 3372
Priscilla 3276
Isis 3258
Hetty 3205
Justin 3170
Micheline 3032
Lucila 3028
Ilene 3005
Gabrielle 2992
Olive 2903
Brianna 2752
Jordan 2720
James 2700
Xavier 2584
Demetra 2485
Gaynor 2460
Anthony 2416
Joshua 2388
Birgit 2387
Aubine 2344
Alexis 2312
Darline 2159
Rosamund 2039
Cindie 1977
Malik 1946
Sarah 1920
Gabriel 1870
Karee 1842
Lindie 1774
Kennedy 1760
Michael 1754
Verile 1752
Marlie 1577
Patrica 1541
Eirena 1517
Irene 1476
Jaylen 1440
Dorisa 1085
Felicle 1075
Angel 979
Cinnamon 874
Clarine 859
Carie 852
Ileana 800
George 702
Makayla 535
Noel 490
Stephine 477
Aaliyah 452
Bernardine 443
Millicent 376
Zola 265
Buffy 226
Christian 94
Tyler 92
Caleb 73
Dorelia 15
--------------------Top 10 score players are--------------------
Players information
Player Name High Score
Destiny 4991
Eimile 4974
Mildred 4972
Hannah 4830
Kaila 4823
Conni 4696
Laila 4636
Persis 4611
Doralia 4551
Jasmine 4391
Sydney 4348
note: please include your input.txt file in the same folder in which main java class file there.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.