1

The basic idea of this assignment is to create a program that can provide a summary and identify who won a "match" in a sports event (football, basketball, Soccer, baseball, etc.)

**This is my code:**
`import java.util.Scanner;

public class Team {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Ask questions about the game type etc.
        System.out.println("Please enter game name: ");
        String gameName = sc.next();
        System.out.println("Please enter " + gameName + " team 1 name: ");
        String t1N = sc.next();
        System.out.println("Please enter " + gameName + " team 2 name: ");
        String t2N = sc.next();
        System.out.println("What is a score in " + gameName + " called? ");
        String scoreName = sc.next();
        System.out.println("How many points per " + scoreName + " in " + gameName + "?");
        int scoreValue = sc.nextInt();
        System.out.println("What is a period in " + gameName + " called?");
        String periodName = sc.next();
        System.out.println("How many " + periodName + " in " + gameName + "?");
        int numberOfPeriods = sc.nextInt();
        int sum1 = 0;
        int sum2 = 0;
        for (int i = 1; i <= numberOfPeriods; i++) {
            System.out.println(periodName + " #" + i);
            System.out.println("How many " + scoreName + " for " + t1N + "?");
            int numberOfScoresT1[] = new int[sc.nextInt()];
            System.out.println("How many " + scoreName + " for " + t2N + "?");
            int numberOfScoresT2[] = new int[sc.nextInt()];

            for (int counter = 0; counter < numberOfScoresT1.length; counter++)
                sum1 += numberOfScoresT1[counter];

            for (int counter = 0; counter < numberOfScoresT1.length; counter++)
                sum2 += numberOfScoresT2[counter];
        }
        System.out.println("Team 1 scored " + sum1 + " team 2 scored " + sum2);

    }`

This is the error I'm receiving: Please enter game name: Football Please enter Football team 1 name: Dolphins Please enter Football team 2 name: Jaguars What is a score in Fotball called? Touchdown How many points per Touchdown in Fotball? 7 What is a period in Fotball called? Quarter How many Quarter in Fotball? 4 Quarter #1 How many Touchdown for Dolphins? 3 How many Touchdown for Jaguars? 2 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at Team.main(Team.java:36)

I recognize that the arrays I'm using are in the for loop, and I think thats what is causing the problem, but i'm not sure how to fix it.

This is a sample output is supposed to look like: Quarter #1: How many Touchdowns for Dolphins? 2 How many Touchdowns for Chargers? 1

Quarter #2: How many Touchdowns for Dolphins? 0 How many Touchdowns for Chargers? 1

Quarter #3: How many Touchdowns for Dolphins? 0 How many Touchdowns for Chargers? 2

Quarter #4: How many Touchdowns for Dolphins? 3 How many Touchdowns for Chargers? 0

Football Game Results:

Dolphins scored 5 Touchdowns for a score of 35 Chargers scored 4 Touchdowns for a score of 28

Dolphins Win by 7 points!

Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
R. Brown
  • 57
  • 6

2 Answers2

1
for (int counter = 0; counter < numberOfScoresT1.length; counter++)
                sum2 += numberOfScoresT2[counter];

Should the second parameter of the loop be

for (int counter = 0; counter < numberOfScoresT2.length; counter++)

seeing as you are accessing numberOfScoresT2 array in the body.

Norman Bentley
  • 640
  • 5
  • 20
0

Your inner for loops should be as follows

        for (int counter = 0; counter < numberOfScoresT1.length; counter++)
            sum1 += numberOfScoresT1[counter];

        for (int counter = 0; counter < numberOfScoresT2.length; counter++)
            sum2 += numberOfScoresT2[counter];

You have used length of array numberOfScoresT1 instead of array numberOfScoresT2 in second inner for loops.

import java.util.Scanner;

public class Team {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Ask questions about the game type etc.
        System.out.println("Please enter game name: ");
        String gameName = sc.next();
        System.out.println("Please enter " + gameName + " team 1 name: ");
        String t1N = sc.next();
        System.out.println("Please enter " + gameName + " team 2 name: ");
        String t2N = sc.next();
        System.out.println("What is a score in " + gameName + " called? ");
        String scoreName = sc.next();
        System.out.println("How many points per " + scoreName + " in " + gameName + "?");
        int scoreValue = sc.nextInt();
        System.out.println("What is a period in " + gameName + " called?");
        String periodName = sc.next();
        System.out.println("How many " + periodName + " in " + gameName + "?");
        int numberOfPeriods = sc.nextInt();
        int sum1 = 0;
        int sum2 = 0;

        int numberOfScoresT1[] = new int[numberOfPeriods];
        int numberOfScoresT2[] = new int[numberOfPeriods];

        for (int i = 0; i <numberOfPeriods; i++) {
            System.out.println(periodName + " #" + i);

            System.out.println("How many " + scoreName + " for " + t1N + "?");
            numberOfScoresT1[i] = sc.nextInt();

            System.out.println("How many " + scoreName + " for " + t2N + "?");
            numberOfScoresT2[i] = sc.nextInt();

        }
        sc.close();

        for (int counter = 0; counter < numberOfPeriods; counter++) {
            sum1 += numberOfScoresT1[counter];
            sum2 += numberOfScoresT2[counter];
        }

        System.out.println("Team 1 scored " + sum1 + " team 2 scored " + sum2);

    }
}
Geeth Lochana
  • 164
  • 13
  • I changed the inner for loop to what you were saying, and now my problem is that the sum1 and sum2 are not actually summing the total of the array. – R. Brown Apr 26 '16 at 22:53
  • This is an output: Quarter #1 How many Touchdown for Lions? 2 How many Touchdown for Patriots? 3 Quarter #2 How many Touchdown for Lions? 1 How many Touchdown for Patriots? 0 Quarter #3 How many Touchdown for Lions? 1 How many Touchdown for Patriots? 4 Quarter #4 How many Touchdown for Lions? 1 How many Touchdown for Patriots? 3 Football Game Results: --------------------------------- Lions scored 0 Touchdown for a score of 0 Patriots scored 0 Touchdown for a score of 0 Lions win by 0 points! – R. Brown Apr 26 '16 at 22:56
  • I have submmited a complete code. I think you are looking for this. If not let use know your primary requirement. – Geeth Lochana Apr 26 '16 at 23:50