How can I fix the error and complete my code?

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter3: Using Methods, Classes, And Objects
Section: Chapter Questions
Problem 1GZ
icon
Related questions
Question

How can I fix the error and complete my code?

In Java.

LeagueDriver.java:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class LeagueDriver {
public static void main(String[] args) {
Scanner sc = null;
try {
sc = new Scanner(new File("Players.txt"));
} catch (FileNotFoundException e) {
System.err.println("No such file");
System.exit(-1);
}
  
League l = new League();
while(sc.hasNext()) {
String temp[] = sc.nextLine().split(", ");
l.addPlayer(new Player(temp[0],temp[1],Integer.parseInt(temp[2])));
}
l.printLeague();
System.out.println();
l.printAverageScore();
}
}

Player.java:

public class Player {
private String name,team;
private int score;
public Player(String n,String t,int s) {
setName(n);
setTeam(t);
setScore(s);
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the team
*/
public String getTeam() {
return team;
}
/**
* @param team the team to set
*/
public void setTeam(String team) {
this.team = team;
}
/**
* @return the score
*/
public int getScore() {
return score;
}
/**
* @param score the score to set
*/
public void setScore(int score) {
this.score = score;
}
  
@Override
public String toString() {
String s = String.format("%s",name);
return s;
}
}

League.java:

import java.util.ArrayList;

public class League {
private ArrayList<Player> players;
  
public League() {
players = new ArrayList<Player>();
}
  
void addPlayer(Player newPlayer) {
players.add(newPlayer);
}
  
void printLeague() {
for(Player p:players) {
System.out.println(p);
}
}
  
void printAverageScore() {
double sum = 0;
for(Player p:players) {
sum += p.getScore();
}
System.out.printf("the average field goals made for the whole league: %.2f",(sum/(float)players.size()));
}
}

Players.txt

Steph Curry, Golden State Warriors, 498,
LeBron James, Cleveland Cavaliers, 484,
Russell Westbrook, Oklahoma City Thunder, 459,
James Harden, Houston Rockets, 449,
Jayson Tatum, Boston Celtics, 450,
Bruce Brown, Brooklyn Nets, 470,

Project 1: Sports League Management Program
Objectives
• Familiarize yourself with Files in Java
• Learn to use ArrayLists
• Practice with Strings
Create a program to manage a League
For this program, create a class League that includes the name of the league and a list of all the players in the
league. (ArrayLists will be covered on February 14th but you can also read ahead to zyBook chapter "1.32 Array
Lists".) You will also need to keep track of the number of players in your league. In addition to the getters/setters
for the class, it will also contain the following methods:
1. void addPlayer(Player newPlayer) - This method must be used to add new players to the league. Helpful note: T
his may be a good place to increment the counter for the number of players currently in the league.
2. printleague() - Print a list of all the players in your league. Make sure to use the Player class' toString()
method for this.
3. printAverageScore() - Calculate and print the average of all the players scores in the league.
Player class
The Player class will only contain the name of the player, the name of the team and the number of field goals made
by the player. For this class, you need the constructor, getters/setters, and the toString() method.
League: Driver class
The driver class (LeagueDriver) will contain the main and must contain the following functionality:
• Read in the player information from an external text file. The name of the text file must be passed into the
program through the command line arguments.
• Use a scanner to read in the file and to obtain the name, team name, and total field goals made for each player
• Use a scanner to read in the file and to obtain the name, team name, and total field goals made for each player
in the file.
Invoke the printLeague() method to print a list of all the players in that league.
• Invoke the printAverageScore() method to display the average field goals made for the whole league.
Reading the Player information from a file
• Create a list of players (name, team, field goals) in a file on your computer. This file will be created as a
separate text file in a text editor such as Sublime. For example, you may create a file (Players.txt) that contains
the following:
Steph Curry, Golden State Warriors, 498,
LeBron James, Cleveland Cavaliers, 484,
Russell Westbrook, Oklahoma City Thunder, 459,
Transcribed Image Text:Project 1: Sports League Management Program Objectives • Familiarize yourself with Files in Java • Learn to use ArrayLists • Practice with Strings Create a program to manage a League For this program, create a class League that includes the name of the league and a list of all the players in the league. (ArrayLists will be covered on February 14th but you can also read ahead to zyBook chapter "1.32 Array Lists".) You will also need to keep track of the number of players in your league. In addition to the getters/setters for the class, it will also contain the following methods: 1. void addPlayer(Player newPlayer) - This method must be used to add new players to the league. Helpful note: T his may be a good place to increment the counter for the number of players currently in the league. 2. printleague() - Print a list of all the players in your league. Make sure to use the Player class' toString() method for this. 3. printAverageScore() - Calculate and print the average of all the players scores in the league. Player class The Player class will only contain the name of the player, the name of the team and the number of field goals made by the player. For this class, you need the constructor, getters/setters, and the toString() method. League: Driver class The driver class (LeagueDriver) will contain the main and must contain the following functionality: • Read in the player information from an external text file. The name of the text file must be passed into the program through the command line arguments. • Use a scanner to read in the file and to obtain the name, team name, and total field goals made for each player • Use a scanner to read in the file and to obtain the name, team name, and total field goals made for each player in the file. Invoke the printLeague() method to print a list of all the players in that league. • Invoke the printAverageScore() method to display the average field goals made for the whole league. Reading the Player information from a file • Create a list of players (name, team, field goals) in a file on your computer. This file will be created as a separate text file in a text editor such as Sublime. For example, you may create a file (Players.txt) that contains the following: Steph Curry, Golden State Warriors, 498, LeBron James, Cleveland Cavaliers, 484, Russell Westbrook, Oklahoma City Thunder, 459,
Steph Curry, Golden State Warriors, 498,
LeBron James, Cleveland Cavaliers, 484,
Russell Westbrook, Oklahoma City Thunder, 459,
James Harden, Houston Rockets, 449,
• Read the file into your program. Lecture on Feb. 16th will cover how to use external files in your program; this
topic is also covered in Ch. 1.36 of your zyBook.
• Since your final submission should have at least 6 players, this file should have at least six players too. Please
do NOT make 6 separate Player objects in the League class. You have to use ArrayLists for this project. Since
we will use ArrayLists, your program should be able to work with any number of players.
Here's a demonstration video of how the finished project should execute.
(Note that your code should be able to handle many more than 4 players.)
МaсВook-Prо:
OMacBook-Pro:c
a$ javac LeagueDriver.java
ea$ java LeagueDriver
Exception in thread "main" java.lang. NumberFormatException: For input string: "449,
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException
•java:67)
at java.base/java.lang. Integer.parseInt (Integer.java:668)
at java.base/java.lang. Integer.parseInt (Integer.java:786)
at LeagueDriver.main(LeagueDriver.java:18)
Transcribed Image Text:Steph Curry, Golden State Warriors, 498, LeBron James, Cleveland Cavaliers, 484, Russell Westbrook, Oklahoma City Thunder, 459, James Harden, Houston Rockets, 449, • Read the file into your program. Lecture on Feb. 16th will cover how to use external files in your program; this topic is also covered in Ch. 1.36 of your zyBook. • Since your final submission should have at least 6 players, this file should have at least six players too. Please do NOT make 6 separate Player objects in the League class. You have to use ArrayLists for this project. Since we will use ArrayLists, your program should be able to work with any number of players. Here's a demonstration video of how the finished project should execute. (Note that your code should be able to handle many more than 4 players.) МaсВook-Prо: OMacBook-Pro:c a$ javac LeagueDriver.java ea$ java LeagueDriver Exception in thread "main" java.lang. NumberFormatException: For input string: "449, at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException •java:67) at java.base/java.lang. Integer.parseInt (Integer.java:668) at java.base/java.lang. Integer.parseInt (Integer.java:786) at LeagueDriver.main(LeagueDriver.java:18)
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Avoiding deadlock
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT