Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: I'm unsure of what is wrong with this code

  1. #1
    Junior Member
    Join Date
    Apr 2020
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default I'm unsure of what is wrong with this code

    I have some homework and thought that i got it correct. but when i compile i get the below error. it shows up after i enter my 8 numbers. Also fair warning, i'm not sure why some of the lines aren't indented properly because that isn't how they show up in my code.

    Enter a batting average:
    .299
    Enter a batting average:
    .157
    Enter a batting average:
    .242
    Enter a batting average:
    .203
    Enter a batting average:
    .198
    Enter a batting average:
    .333
    Enter a batting average:
    .270
    Enter a batting average:
    .190
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
    at BattingAverage.main(BattingAverage.java:60)


    import java.util.Scanner;
    public class BattingAverage
    {
    	public static void main(String args[])
    	{
    		Scanner s = new Scanner(System.in);
    		// Declare a named constant for array size here.
    		int arraySize = 8;
     
    		// Declare array here.
    		double[] averages = new double[arraySize];
     
    		// Use this integer variable as your loop index.
    		int i; 
     
    		// Use this variable to store the batting average input by user.
    		double x;
     
    		// String version of batting average input by user.
    		String averageString; 
     
    		// Use these variables to store the minimim and maximum batting averages.
    		double min, max;
     
    		// Use these variables to store the total and the average.
    		double total, average; 
     
    		// Write a loop to get batting averages from user and assign to array.
    		for(i=0; i < arraySize; i++){
    		   System.out.println("Enter a batting average: ");
     
    		   averageString  = s.nextLine();
    		   x = Double.parseDouble(averageString);
    		   // Assign value to array.
    		   averages[i] = x;
            }
    		// Assign the first element in the array to be the minimum and the maximum.
    		min = averages[0];
    		max = averages[0];
    		// Start out your total with the value of the first element in the array.
    		total = averages[0]; 
    		// Write a loop here to access array values starting with averages[1]
    		for(i=1; i < arraySize; i++){
    		   // Within the loop test for minimum and maximum batting averages.
    		   if(averages[i] > max){
                   max = averages[i];
               }
               if(averages[i] < min){
                   min = averages[i];
               }
    		   // Also accumulate a total of all batting averages.
    		   total = total + averages[i];
            }
     
     
    		// Calculate the average of the 8 averages.
    		average = total / arraySize;
     
    		// Print the averages stored in the averages array. 
    		System.out.println(averages[i]);
    		// Print the maximum batting average, minimum batting average, and average batting average. 
    		System.out.println("Minimum batting average is " +min);
            System.out.println("Maximum batting average is " +max);
            System.out.println("Average batting average is " +average);
    		System.exit(0);
     
    	}
    }
    Last edited by Imeerie; April 7th, 2020 at 11:12 AM. Reason: Solved

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I'm unsure of what is wrong with this code

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
    at BattingAverage.main(BattingAverage.java:60)
    At line 60 the code used an index (8) that was past the end of the array. The valid indexes for An array with 8 elements is 0 to 7.

    Look at line 60 and see why it used an index of 8.

    Note: Defining for loop control indexes outside of the for loop is a poor technique.
    It is recommended to do it this way:
       for(int i=0; i<max;i++)     // define i inside the for loop
    If you want to save a value of the index, use another variable.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Imeerie (April 7th, 2020)

  4. #3
    Junior Member
    Join Date
    Apr 2020
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: I'm unsure of what is wrong with this code

    Line 60 is

    System.out.println(averages[i]);

    i am not sure what is wrong with it.


    so far as declaring i inside the loop, i have noticed that is how everyone shows it in tutorial videos but the class im taking on cenegage mindtap supplied most of the variables and will count it wrong if i don't use them.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I'm unsure of what is wrong with this code

    that is how everyone shows it
    They are wrong. That is NOT the right way to code for loops.

    what is wrong with it.
    i has a value that is past the end of the array. The max array index is 7 for an array with 8 elements.
    What value do you want to print? Where is that value located?
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Imeerie (April 7th, 2020)

  7. #5
    Junior Member
    Join Date
    Apr 2020
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: I'm unsure of what is wrong with this code

    sorry for the misunderstanding, what i meant was that declaring the variable in the for loop is how i have seen people doing it, only that the class i'm taking will count my answer wrong if i do that since it supplied the variables.

    i am trying to print the entire array, and i just realized that what i wrote is trying to print a single value of an array. and since i last equaled 8 that is what it is trying to print.

    EDIT: i got the program to work! thanks for the help!
    Last edited by Imeerie; April 7th, 2020 at 11:11 AM. Reason: solved

Similar Threads

  1. Unsure
    By #0Prog in forum Java Theory & Questions
    Replies: 2
    Last Post: June 9th, 2018, 04:28 PM
  2. Unsure of what happened, but it can't be good
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: May 23rd, 2012, 04:07 PM
  3. Unsure where to start with networking
    By totesmagotes in forum Java Networking
    Replies: 3
    Last Post: May 9th, 2012, 04:52 PM
  4. In a bit of a pickle, unsure of what I should do
    By MysticDeath in forum What's Wrong With My Code?
    Replies: 16
    Last Post: October 31st, 2009, 09:10 AM
  5. :!! Unsure how to set this up/ Still learning java loops!!!!!
    By raidcomputer in forum Loops & Control Statements
    Replies: 4
    Last Post: September 29th, 2009, 10:28 AM

Tags for this Thread