Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

Fantasy Football Problem near the end of Thin exereise is very similar o the Gradeook.java e Chapter 6 of your textbook. ake sure you have watched the video "GradeBook and Fantany Football found inhArrays Part2" section. The Textbook' example doe s many of the ame things that you will need to do in your Eantasy ootball program. However, instead of a teacher grade book, ve have the folloving fantay footbali scores: Team Name Blind_Pigeons Week 1 123 54 70 Week 2 Week 3 95 121 ave 113 98 83 ave 78 74 103 ave 82 81 ave 10 Bucking Broncos 108 119 eekly Create a FantasyFootba11.ava class to read in the information in the above tabie On? th differences in the overall structure is the fact that ?n Gradebook re didn'thave any Seudent nanes. In Fantasy Footbali we want a nane for each tea This wiii require adding an additional array t?store the names?£ the teams. Yo vill implify your efforts if the names of your teams do not include spaces. Then you can use next instead of nextLine and your code ill be simplified You will get most of the credit for thA5 assignment even if your output is not conpletely ali technique from GradeBook.java you il1 get a ragged looking table in the above image Note that if you use the Getting the table aligned as seen in the above image will give you the last5 points on this problem You can get the alignment to look somehat OK, if you use tab characters to go to new columns and round the double values to integer value with cast operation However, you can get perfect alignment using the 3ystem.printf.approach re have shon before Consider the following examples of printf and println: String ?-"something". double d5.4321 int 3456: 3ystem.out-printf"20s",) ield of 20 characters, right justified string Systen. out .printf["2-20s"? s); // Field oE 20 characters, left justified string ystem.out.printf("8d", x) //Field of charactes, xight justified integer System, out .prinf{"I-8d", ?); //Fieldof 8 characters, left justified integer ystem.out.println) Go to a ne line You should tart vith the "Starting Code" shon below

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