JAVA problem: ******************************************************************
ID: 3911183 • Letter: J
Question
JAVA problem:
****************************************************************************************************************************************************************
My Code: What is wrong with my code? After entering in the number of weeks an error message occurs.
Output:
Enter number of teams:
4
Enter number of weeks:
3
Enter team name
Exception in thread "main" java.lang.NullPointerException
at fantasy_football.FantasyFootball.enterInData(FantasyFootball.java:37)
at fantasy_football.FantasyFootball.main(FantasyFootball.java:78)
***************************************************************************************************************************************************************
package fantasy_football;
import java.util.Scanner;
public class FantasyFootball {
private int numberOfTeams;
// Same as teamAverage.length.
private int numberOfWeeks;
// Same as weekAverage.length.
private int[][] scores;
// numberOfTeams rows and numberOfWeeks columns.
private double[] weekAverage;
// contains an entry for each week
private double[] teamAverage; // contains an entry for each team
private String[] teamName;
// contains an entry for each team
public void enterInData() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter number of teams:");
numberOfTeams = keyboard.nextInt();
System.out.println("Enter number of weeks:");
numberOfWeeks = keyboard.nextInt();
// ************** Fill in Code ***************
// Allocate array memory for teamName to store the team names.
// Allocate array memory for scores (2 dimensional array) to store a
// score for each team for each week.
for (int team = 0; team < numberOfTeams; team++) {
System.out.println("Enter team name");
// ************* Fill in Code **************
// Read in Team name and store it in teamName
String name = keyboard.nextLine();
teamName[team] = name;
for (int week = 0; week < numberOfWeeks; week++) {
System.out.println("Enter score for team " + teamName[team]);
System.out.println("on week number " + (week + 1));
// ************ Fill in Code ***************
// Read in a score and store it in the proper spot in the scores array
int score = keyboard.nextInt();
scores[team][week] = score;
}
}
// Read in a score and store it in the proper spot in the scores array
}
public void fillTeamAverage() {
// ********* Fill in Code *************
// Allocate memory for the teamAverage.
}
// // Each entry in this array will contain the
// average weekly score for a given team.
public void fillWeekAverage() {
// *********** Fill in Code *************
// Allocate memory for the weekAverage
}
// instance variable.
// Each entry in this array will contain the average of all teams for a given
// week.
public void display() {
// ********* Fill in Code ****************
// This method will print out the display that was shown above.
// At this point all of the information can be found in the
// private instance variables of the FantasyFootball class
}
public static void main(String[] args) {
FantasyFootball f = new FantasyFootball();
f.enterInData();
f.fillTeamAverage();
f.fillWeekAverage();
f.display();
}
}
What the output should look like:
Enter number of teams: 4
Enter number of weeks: 3
Enter team name: Blind_Pigeons
Enter score for team Blind_Pigeons on week number 1: 123
Enter score for team Blind_Pigeons on week number 2: 95
Enter score for team Blind_Pigeons on week number 3: 121
Enter team name: Spittn_Lamas
Enter score for team Spittn_Lamas on week number 1: 54
Enter score for team Spittn_Lamas on week number 2: 98
Enter score for team Spittn_Lamas on week number 3: 83
Enter team name: Nut_Zippers
Enter score for team Nut_Zippers on week number 1: 70
Enter score for team Nut_Zippers on week number 2: 74
Enter score for team Nut_Zippers on week number 3: 103
Enter team name: Bucking_Broncos
Enter score for team Bucking_Broncos on week number 1: 108
Enter score for team Bucking_Broncos on week number 2: 119
Enter score for team Bucking_Broncos on week number 3: 81
******************************** output from display() with ragged output ********************************
Blind_Pigeons 123 95 121 Ave = 113
Spittn_Lamas 54 98 83 Ave = 78
Nut_Zippers 70 74 103 Ave = 82
Bucking_Broncos 108 119 81 Ave = 102
Weekly Ave: 88 96 97
******************************** output from display() using printf ********************************
Team Name Week 1 Week 2 Week 3
Blind_Pigeons 123 95 121 ave = 113
Spittn_Lamas 54 98 83 ave = 78
Nut_Zippers 70 74 103 ave = 82
Bucking_Broncos 108 119 81 ave = 102
Weekly Ave: 88 96 97
Explanation / Answer
import java.util.Scanner;
import java.util.ArrayList;
public class FantasyFootball
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
ArrayList<String> players = new ArrayList<String>();
String indexStr;
int index = 0;
players.add("Peyton Manning");
players.add("Ladanian Tomlinson");
players.add("Reggie Bush");
System.out.print("Enter a number between 1 and 3: ");
indexStr = stdIn.nextLine();
try
{
index = Integer.parseInt(indexStr);
System.out.println("Entered index OK.");
}
catch (NumberFormatException e)
{
System.out.println("Entered index wasn't an integer");
}
try
{
System.out.println(players.get(index - 1));
}
catch (IndexOutOfBoundsException e)
{
System.out.println(
"Can't access players[" + (index - 1) + "]");
}
System.out.println("done");
} // end main
} // end class FantasyFootball
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.