1

I am trying to figure out how to add in a new feature to this fantasy football program. I want to be able to have the users put in names for their teams rather than just be "team 1", "team 2" etc. I am trying to create a string array and

  1. Ask the user for the team name
  2. List the team names in the next questions asking for scores on the weeks
  3. List the team names in the final results tally at the end of the program

I have not been able to figure out how to do this after a lot of searching and reading. I currently ask the user for the team name in the for loop where I ask for scores of each week and that does not seem to be the correct place to put it. Any help would appreciated.

import java.util.Scanner;

public class fantasyFootball
{

  private int numberOfTeams; // Same as teamAverage.length.
  private int numberOfWeeks; // Same as weekAverage.length.

  private int[][] score; //numberOfTeams rows and numberOfWeeks columns.
  private int[] teamAverage;
  private int[] scoreAverage;

  public static void main(String[] args)
  {
    fantasyFootball book = new fantasyFootball( );
    book.display();
  }

  public fantasyFootball(int[][] a)
  {
    if (a.length == 0 || a[0].length == 0)
    {
        System.out.println("Empty score records. Aborting.");
        System.exit(0);
    }

    numberOfTeams = a.length;
    numberOfWeeks = a[0].length;
    fillScore(a);
    fillTeamAverage( );
    fillScoreAverage( );
  }

  public fantasyFootball(fantasyFootball book)
  {
    numberOfTeams = book.numberOfTeams;
    numberOfWeeks = book.numberOfWeeks;
    fillScore(book.score);
    fillTeamAverage( );
    fillScoreAverage( );
  }

  public fantasyFootball( )
  {
    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( );

    score = new int[numberOfTeams][numberOfWeeks];

    for (int teamName = 1; 
            teamName <= numberOfTeams; teamName++)
        for (int weekNumber = 1; 
                weekNumber <= numberOfWeeks; weekNumber++)
        {
            System.out.println("Enter team name ");
            teamName.add(keyboard.nextLine()); 
            System.out.println("Enter score for team "
                    + teamName);
            System.out.println("on week number " + weekNumber);
            score[teamName - 1][weekNumber - 1] =
                    keyboard.nextInt( );
        }

    fillTeamAverage( );
    fillScoreAverage( );
  }

  private void fillScore(int[][] a)
  {
    score = new int[numberOfTeams][numberOfWeeks];

    for (int teamName = 1; 
            teamName <= numberOfTeams; teamName++)
    {
        for (int weekNumber = 1; 
                weekNumber <= numberOfWeeks; weekNumber++)
            score[teamName][weekNumber] = 
            a[teamName][weekNumber];
    }
  }

/**
  Fills the array teamAverage using the data from the array score.
 */
  private void fillTeamAverage( )
  {
    teamAverage = new int[numberOfTeams];

    for (int teamName = 1; 
            teamName <= numberOfTeams; teamName++)
    {//Process one teamName:
        int sum = 0;
        for (int weekNumber = 1; 
                weekNumber <= numberOfWeeks; weekNumber++)
            sum = sum + score[teamName - 1][weekNumber - 1];
        //sum contains the sum of the week scores for team number teamName.
        teamAverage[teamName - 1] = sum/numberOfWeeks;
        //Average for team teamNumber is teamAverage[teamNumber - 1]
    }
  }

/**
  Fills the array scoreAverage using the data from the array score.
 */
  private void fillScoreAverage( )
  {
    scoreAverage = new int[numberOfWeeks];

    for (int weekNumber = 1; weekNumber <= numberOfWeeks; weekNumber++)
    {//Process one week (for all teams):
        int sum = 0;
        for (int teamName = 1; 
                teamName <= numberOfTeams; teamName++)
            sum = sum + score[teamName - 1][weekNumber - 1];
        //sum contains the sum of all team scores on week number weekNumber.
        scoreAverage[weekNumber - 1] = sum/numberOfTeams;
        //Average for week weekNumber is the value of weekAverage[weekNumber - 1]
    }
  }

  public void display( )
  {
    for (int teamName = 1; 
            teamName <= numberOfTeams; teamName++)

    {//Display for one teamNumber:
        System.out.print("Team " + teamName + " Weeks: ");
        for (int weekNumber = 1; 
                weekNumber <= numberOfWeeks; weekNumber++)
            System.out.print(score[teamName - 1][weekNumber - 1] + " ");
        System.out.println(" Ave = " + teamAverage[teamName - 1] );
    }

    System.out.println("Week averages: ");
    for (int weekNumber = 1; weekNumber <= numberOfWeeks; weekNumber++)
        System.out.print("Week " + weekNumber 
                + " Ave = " + scoreAverage[weekNumber - 1] + " ");
    System.out.println( );
}

  private void teamName()
  {
    Scanner keyboard = new Scanner(System.in);
    String[] teamName = new String[3];

  }

}

2
  • 1
    just use a ArrayList<String> Commented Jul 9, 2014 at 2:48
  • Or you can just use a Map to define a whole team object, with key-value pairs, like name, id, week, score etc Commented Jul 9, 2014 at 2:55

4 Answers 4

1

Here is a solution using ArrayList to store team Names:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class fantasyFootball {

    private List<String> teamNames = new ArrayList<String>();
    private int numberOfTeams; // Same as teamAverage.length.
    private int numberOfWeeks; // Same as weekAverage.length.

    private int[][] score; // numberOfTeams rows and numberOfWeeks columns.
    private int[] teamAverage;
    private int[] scoreAverage;

    public static void main(String[] args) {
        fantasyFootball book = new fantasyFootball();
        book.display();
    }

    public fantasyFootball(int[][] a) {
        if (a.length == 0 || a[0].length == 0) {
            System.out.println("Empty score records. Aborting.");
            System.exit(0);
        }

        numberOfTeams = a.length;
        numberOfWeeks = a[0].length;
        fillScore(a);
        fillTeamAverage();
        fillScoreAverage();
    }

    public fantasyFootball(fantasyFootball book) {
        numberOfTeams = book.numberOfTeams;
        numberOfWeeks = book.numberOfWeeks;
        fillScore(book.score);
        fillTeamAverage();
        fillScoreAverage();
    }

    public fantasyFootball() {
        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();

        score = new int[numberOfTeams][numberOfWeeks];

        for (int teamName = 1; teamName <= numberOfTeams; teamName++){
            System.out.println("Enter team " + teamName + " name ");
            String name = keyboard.next();
            teamNames.add(name);
            for (int weekNumber = 1; weekNumber <= numberOfWeeks; weekNumber++) {
                System.out.println("Enter score for team " + teamName + " on week number " + weekNumber);
                score[teamName - 1][weekNumber - 1] = keyboard.nextInt();
            }
        }

        fillTeamAverage();
        fillScoreAverage();
    }

    private void fillScore(int[][] a) {
        score = new int[numberOfTeams][numberOfWeeks];

        for (int teamName = 1; teamName <= numberOfTeams; teamName++) {
            for (int weekNumber = 1; weekNumber <= numberOfWeeks; weekNumber++)
                score[teamName][weekNumber] = a[teamName][weekNumber];
        }
    }

    /**
     * Fills the array teamAverage using the data from the array score.
     */
    private void fillTeamAverage() {
        teamAverage = new int[numberOfTeams];

        for (int teamName = 1; teamName <= numberOfTeams; teamName++) {// Process
                                                                        // one
                                                                        // teamName:
            int sum = 0;
            for (int weekNumber = 1; weekNumber <= numberOfWeeks; weekNumber++)
                sum = sum + score[teamName - 1][weekNumber - 1];
            // sum contains the sum of the week scores for team number teamName.
            teamAverage[teamName - 1] = sum / numberOfWeeks;
            // Average for team teamNumber is teamAverage[teamNumber - 1]
        }
    }

    /**
     * Fills the array scoreAverage using the data from the array score.
     */
    private void fillScoreAverage() {
        scoreAverage = new int[numberOfWeeks];

        for (int weekNumber = 1; weekNumber <= numberOfWeeks; weekNumber++) {// Process
                                                                                // one
                                                                                // week
                                                                                // (for
                                                                                // all
                                                                                // teams):
            int sum = 0;
            for (int teamName = 1; teamName <= numberOfTeams; teamName++)
                sum = sum + score[teamName - 1][weekNumber - 1];
            // sum contains the sum of all team scores on week number
            // weekNumber.
            scoreAverage[weekNumber - 1] = sum / numberOfTeams;
            // Average for week weekNumber is the value of
            // weekAverage[weekNumber - 1]
        }
    }

    public void display() {
        for (int teamName = 1; teamName <= numberOfTeams; teamName++)

        {// Display for one teamNumber:
            System.out.print("Team " + teamNames.get(teamName-1) + " Weeks: ");
            for (int weekNumber = 1; weekNumber <= numberOfWeeks; weekNumber++)
                System.out.print(score[teamName - 1][weekNumber - 1] + " ");
            System.out.println(" Ave = " + teamAverage[teamName - 1]);
        }

        System.out.println("Week averages: ");
        for (int weekNumber = 1; weekNumber <= numberOfWeeks; weekNumber++)
            System.out.print("Week " + weekNumber + " Ave = "
                    + scoreAverage[weekNumber - 1] + " ");
        System.out.println();
    }

    private void teamName() {
        Scanner keyboard = new Scanner(System.in);
        String[] teamName = new String[3];

    }
}

Output:

Enter number of teams:
3
Enter number of weeks:
2
Enter team 1 name 
MyTeam1
Enter score for team 1 on week number 1
1
Enter score for team 1 on week number 2
2
Enter team 2 name 
MyTeam2
Enter score for team 2 on week number 1
3
Enter score for team 2 on week number 2
4
Enter team 3 name 
MyTeam3
Enter score for team 3 on week number 1
4
Enter score for team 3 on week number 2
6
Team MyTeam1 Weeks: 1 2  Ave = 1
Team MyTeam2 Weeks: 3 4  Ave = 3
Team MyTeam3 Weeks: 4 6  Ave = 5
Week averages: 
Week 1 Ave = 2 Week 2 Ave = 4 
Sign up to request clarification or add additional context in comments.

Comments

0

I would set the team names before getting the scores,

System.out.println("Enter number of teams:");
int numberOfTeams = keyboard.nextInt( );

String[] teamNames = new String[numberOfTeams];
for (int i = 0; i < numberOfTeams; i++) {
  System.out.printf("Enter team name for team %d:%n", 1+i);
  teamNames[i] = keyboard.next();
}

Comments

0
        System.out.println("Enter team name ");
        teamName.add(keyboard.nextLine()); 
        System.out.println("Enter score for team "
                + teamName);

Change to:

        System.out.println("Enter team name ");
        String name = (keyboard.nextLine()); 
        System.out.println("Enter score for team "
                + name);

Also, if you want to store it:

private String[] names = new String[numberOfTeams];
//Then in the above code:
names[teamName] = name;

1 Comment

This does 2 things: stores the entered name. Adds it to an array
0

This is a solution using a simple array of String objects. Add private String[] names; to your list of instance fields, the make the FantasyFootball constructor look like this:

public FantasyFootball()
{
    int numTeams;
    int numWeeks;
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter number of teams: ");
    numTeams = keyboard.nextInt();

    for (int i = 0; i < numTeams; i++)
    {
        System.out.print("Enter the name of team " + (i+1) + ": ");
        names[i] = keyboard.nextLine();
    }

    System.out.print("Enter number of weeks: ");
    numWeeks = keyboard.nextInt();

    names = new String[numTeams];
    scores = new int[numTeams][numWeeks];

    for (int i = 0; i < numTeams; i++)
    {
        for (int j = 0; j < numWeeks; j++)
        {
            System.out.print("Enter the score for " + names[i] + " for week " + (j+1) + ": ");
            scores[i][j] = keyboard.nextInt();
        }
    }

    fillTeamAverage();
    fillScoreAverage();
}

A few programming tips:

Firstly, I would suggest creating a Team object that contains an array of the scores for each week, an int for the team average, and an int for the score average. Then in your fantasyFootball class, just manipulate an array of Team objects, instead of an array for each different field. It makes everything easier to work with in the future.

Class names generally start with capital letters by convention. Nearly everyone follows the convention, so I strongly suggest you do too.

Using the one-liner-so-I-don't-need-braces syntax is shorter admittedly, but it makes for harder to read code and harder to debug code, so I also suggest you use all of the braces, as well as putting opening braces on new lines instead of at the end of the statement line. Again, it will make things much easier down the road.

Lastly, in Java, you can use the + operator to concatenate variables onto strings as you know. But I noticed in your code that you broke some prints down into two calls. You might already know this, but you can use more than one + operator in the same string/print statement, just like using + with numbers.

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.