File PrintPlayerInfo.java contains an incomplete program. The goal of the progra
ID: 3774305 • Letter: F
Question
File PrintPlayerInfo.java contains an incomplete program. The goal of the program is to help users search for statistics of a specific player, stored in files formatted like nba.txt. In particular, in the main loop of the program:
1.The user specifies the input file, output file, and part of a player's name (or the entire name).
2.The program looks for all players whose names contain that part. For each such player, the program prints out the stats of that player.
Complete that program, by defining a printPlayerInfo function, that satisfies the following specs:
- Function printPlayerInfo takes two arguments, called data, player. Argument data is a 2D string array containing the data of the nba.txt spreadsheet. Argument player is a string.
-The function finds all rows in data for which column 0 (in its lower-case version) contains as a substring the string stored in player (also in its lower-case version). For all such rows, it prints the statistics of the corresponding player.
-Your program should not crash under any circumstances. If the file does not exist, or if the file does not follow the expected format for whatever reason, your program should be able to handle it without crashing. In our testing, we may use files formatted very differently than the example nba.txt given here. We may even use files called "nba.txt" that contain different contents than the nba.txt file provided here. We may also test your program in situations where the file does not exist.
IMPORTANT: You are NOT allowed to modify in any way the main function. You are free to define and use auxiliary functions. You are also free to use code written in class, posted on the course website, or available on the lecture slides.
PrintPlayerInfo.java:
nba.txt file:
Example Output:
Explanation / Answer
Note : I have made my own method 'readSpreadsheet' as you have not provided. Please change the main method accordingly.. Also check the path of the filename as per your need.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class PrintPlayerInfo{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (true)
{
try {
System.out.printf(" Enter the name of a file to read: ");
String filename = in.next();//user input
String[][] data;
data = readSpreadsheet(filename);//reasing the file
System.out.printf(" Enter part of a player's name (or q to quit): ");
String player = in.next();//user input
if (player.equals("q"))
{
System.out.printf("Exiting... ");
System.exit(0);
}
printPlayerInfo(data, player);//calling the method to print stats
} catch (Exception e) {
System.err.println("Please check your file");
continue;
}
}
}
//printing the player stats
public static void printPlayerInfo(String[][]data,String player){
for(int i=1;i<data.length;i++){ //looping through the array
int k=0;
for(int j=0;j<data[i].length;j++){
if(data[i][0].toLowerCase().contains(player.toLowerCase())){
if(k==0){
System.out.println("-------------------------------------------------------------");
}
System.out.println(data[0][k]+" : "+data[i][j]);//printing the player info
k++;
}
}
}
}
public static String[][] readSpreadsheet(String fileName) throws Exception {
Scanner input;
String[][] data=null;
input = new Scanner (new File("D://"+fileName+".txt"));
// read the rows and columns for the array
int rows = 0;
int columns = 0;
while(input.hasNextLine())
{
++rows;
String line=input.nextLine();
String colArray[]=line.split(",");
if(columns==0){
columns+=colArray.length;
}
}
data = new String[rows][columns];
// read in the data and storing inside the array
input = new Scanner(new File("D://"+fileName+".txt"));
rows=0;
while(input.hasNextLine())
{
String line=input.nextLine();
String colArray[]=line.split(",");
for(int j = 0; j < colArray.length; j++)
{
data[rows][j] = colArray[j];
}
rows++;
}
return data;
}
}
---------------------------------------------------------output--------------------------------------------------------------------
Enter the name of a file to read: nba
Enter part of a player's name (or q to quit): jord
-------------------------------------------------------------
player : Jordan Hill
games played : 70
minutes per game : 26.8
points : 12.0
field goals made : 5.1
field goal attempts : 11.1
field goal percentage : 45.9
3-pointers made : 0
3-pointers attempted : 0.2
3-pointer percentage : 27.3
free throws made : 1.8
free throws attempted : 2.4
free throws percentage : 73.8
rebounds : 2.5
assists : 5.5
steals : 7.9
blocks : 1.5
-------------------------------------------------------------
player : Jordan Clarkson
games played : 59
minutes per game : 25
points : 11.9
field goals made : 4.5
field goal attempts : 10.1
field goal percentage : 44.8
3-pointers made : 0.6
3-pointers attempted : 2.1
3-pointer percentage : 31.4
free throws made : 2.2
free throws attempted : 2.7
free throws percentage : 82.9
rebounds : 0.9
assists : 2.3
steals : 3.2
blocks : 3.5
-------------------------------------------------------------
player : DeAndre Jordan
games played : 82
minutes per game : 34.4
points : 11.5
field goals made : 4.6
field goal attempts : 6.5
field goal percentage : 71
3-pointers made : 0
3-pointers attempted : 0
3-pointer percentage : 25
free throws made : 2.3
free throws attempted : 5.7
free throws percentage : 39.7
rebounds : 4.8
assists : 10.1
steals : 15
blocks : 0.7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.