-1

So I'm making a program for an assignment in class. We are working with array's. Basically, you have to have three attributes in the class: Number of players (which I made five), an array with the number of at-bats per player, and the number of hits per player.

My goal is to prompt the use to enter the number of at-bats and hits for each of the five players. Then the batting average is calculated. Then I check to see if the player has made the all star team. I then proceed to total value of hits for all the players (Having trouble with this part). Lastly, I put the total hits in smallest value to biggest value using a sorting algorithm.

Here's the code. I put the client in the same class.

    import java.util.Scanner;

    public class Test
    {
      static int numberOfPlayers = 5;
       int [] numberOfHits;
       int [] numberOfAtBats;
       int battingAverage;
       int [] totalHits;

       public Test(int [] numberOfHits, int [] numberOfAtBats)
        {

        this.numberOfAtBats = numberOfAtBats;
        this.numberOfHits = numberOfHits;
        battingAverage = CalcBattingAverageInd(numberOfHits, numberOfAtBats);
        System.out.println("Batting average for this player is " + battingAverage);//print batting average for that player
        CheckForAllStar(battingAverage);//Call check for allstar method
        totalHits = TotalHits(numberOfHits);//Evertime I add a "+" symbol, it responds with an error, so I can't add more to the total

        }
        //Manipulator method
        public int CalcBattingAverageInd(int [] numberOfHits, int [] numberOfAtBats)
        {
        int battingAverage = numberOfAtBats.length/numberOfHits.length;    
        return battingAverage;
        }
        public int [] TotalHits(int [] totalHits)
        {
        totalHits = new int[numberOfHits.length];
        return totalHits;
        }
        public void CheckForAllStar(int battingAverage)
        {
         this.battingAverage = battingAverage;
            if (battingAverage >= 3)
                System.out.println("Your an all-star!");
            else
                System.out.println("Sorry, you didn't make the all-star team");
        }
        public void Sort(int [] totalHits)//Selection Sort method the total Hits
        {
        int temp;
        int max;

        for (int i = 0; i < totalHits.length; i++)
        {
         max = indexOfLargestElement(totalHits, totalHits.length);   

         temp = totalHits[max];
         totalHits[max] = totalHits[totalHits.length - i - 1];
         totalHits[totalHits.length-i-1] = temp;
        }

        }       
      public static int indexOfLargestElement(int [] array, int size)
      {
      int index = 0;
      for(int i = 0; i < size; i++ )
      {
      if (array[i] > array[index])    
          index = i;
      }
      return index;
      }
       public static void main(String args[])
      {
          int check = 0;
          Scanner scan = new Scanner(System.in);
           int  [] inputHits = {};
          int  [] inputAtBats = {};
          while(check < numberOfPlayers)
          {
      System.out.println("There are " + Test.numberOfPlayers + " players");
      for(int i = 0; i < 1; i++)
        {
        System.out.println("Enter the number of at-bat attempts: ");
        inputAtBats[i] = scan.nextInt();
        System.out.println("Now enter the number of Hits: ");
        inputHits[i] = scan.nextInt();
        Test(inputHits, inputAtBats);//Error here: Cannot find symbol
        }
      ++check;
          }
      }
    }

I'm using all variables and arrays of type int because I'm still learning converting between array data types. I commented in the code where I'm having trouble. I made this same program without arrays, and it ran fine. But for some reason, I can't run it with arrays. Any help and correction would be great. Thanks.

  • what exactly is the error that you're getting? – Oli Nov 26 '15 at 17:36
  • At the bottom. It cannot find the constructor I made. The error is: Cannot find symbol. I also am having trouble totaling the number of hits because java won't let me add a '+' symbol. – Carter Hunt Nov 26 '15 at 17:39
  • *"Evertime I add a "+" symbol, it responds with an error, so I can't add more to the total"* An array has a fixed size, you can't append more elements to an existing array using `+`. [You still can combine two arrays](http://stackoverflow.com/questions/80476/how-can-i-concatenate-two-arrays-in-java), but I don't think this is what you want here. – Tom Nov 26 '15 at 17:43

1 Answers1

1
Test(inputHits, inputAtBats);//Error here: Cannot find symbol

you can't use this static call (since you do not have a static method like this) but you have a class like this so why not...

Test t = new Test(inputHits, inputAtBats);
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • Oh, I see. This makes sense. I tried a similar method, but it didn't work. However, this one works fine. Thank you! – Carter Hunt Nov 26 '15 at 17:42
  • : ), other problems, (a runtime error can have 1M reasons.. you need to provided much more info than this...). The best solution I can give you download and learn how to use an IDE as eclipse and the you are home free... – Petter Friberg Nov 26 '15 at 17:45
  • I bet it is on this line: inputAtBats[i] = scan.nextInt();, in this case the error is since you have set your arrays to have size 0... instead of array use a List so you can add without problem – Petter Friberg Nov 26 '15 at 17:49