ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I was tasked to write a program that prompts for 10 numbers, averages them and counts the occurance of the numbers above the average.
I followed examples provided and wrote the following program, but when I run it I get array out of bounds. I understand what the error is, just can't seem to correct it.
Can someone please assist me?
Thanks
package flanagan_ch5_p1;
import javax.swing.JOptionPane;
/**
* <p>Title: Chapter 5 PE 1</p>
*
* <p>Description: Analyzing input with arrays</p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: NOVA, ELI, ITP 120, Spring 06</p>
*
* @author Debora A. Flanagan
* @version 1.0
*/
public class flanagan_ch5_p1 {
/**Main method*/
public static void main(String[] args) {
final int TOTAL_NUMBERS = 10;
int [ ] numbers = new int[ TOTAL_NUMBERS ];
int sum = 0;
//Request numbers from user and convert to interger
for (int i = 0; i < TOTAL_NUMBERS; i++){
String numString = JOptionPane.showInputDialog(null, "Enter a number",
"P.E. 5.1 Input", JOptionPane.QUESTION_MESSAGE);
//Convert string to integer
numbers [ i ] = Integer.parseInt(numString);
}
//Compute their average
sum += numbers [TOTAL_NUMBERS];
int average = sum / 10;
//Find the numbers above average and count how many
int numOfAbove =0;
int count = 0;
for (int i =0 ; i < TOTAL_NUMBERS; i++) {
if (numbers [i] > average) count++;
}
//Prepare the results
String output = "The numbers entered were ";
for (int i = 0; i < TOTAL_NUMBERS; i++) {
output += numbers [i] + " ";
}
output += "\nThe average of the numbers is " + average;
output += "\nThe occurance count of the numbers above average is " + count;
//Display the result
JOptionPane.showMessageDialog(null, output, "P.E. 5.1 Output", JOptionPane.INFORMATION_MESSAGE);
}
}
The array out of bounds exception is thrown when you try to use an index greater than (n - 1), where n = number of elements in the array. You've declared your array to hold TOTAL_NUMBERS elements and in the line above, you're trying to access an index greater than the maximum for that array. Array counting starts from zero, hence for 10 elements, the index of the last element will be 9.
Edit: you really want a loop to be calculating that sum:
Code:
int sum = 0;
for(int i = 0; i < TOTAL_NUMBERS; i++)
{
sum += numbers[i];
}
double avg = sum / TOTAL_NUMBERS;
Also, in future could you use code tags? They make code easier to read!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.