LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-14-2006, 10:27 AM   #1
dflan98783
LQ Newbie
 
Registered: Oct 2005
Location: Stafford, VA
Posts: 6

Rep: Reputation: 0
Unhappy Java newbie Array -out of bounds exception


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);
}
}
 
Old 04-14-2006, 10:33 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by dflan98783
sum += numbers [TOTAL_NUMBERS];
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!

Last edited by Nylex; 04-14-2006 at 10:38 AM.
 
Old 04-14-2006, 10:41 AM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
sum += numbers [TOTAL_NUMBERS];

That line is your problem.

You are creating an array that can be index from 0-9, you are indexing the array with a 10.
 
Old 04-14-2006, 10:43 AM   #4
dflan98783
LQ Newbie
 
Registered: Oct 2005
Location: Stafford, VA
Posts: 6

Original Poster
Rep: Reputation: 0
Thank you for the quick response. I do apologize about the code tags, not sure how to use them.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
java exception: java.lang.NoClassDefFoundError kath Programming 12 05-11-2006 04:37 AM
Java 'Main' exception question magnumbi Programming 2 10-11-2005 12:41 PM
Java getHostAddress Exception -- Suse scherl Programming 1 04-23-2005 03:58 AM
Java getHostAddress Exception scherl SUSE / openSUSE 2 04-22-2005 12:55 AM
Java: try won't catch exception nro Programming 3 09-15-2004 11:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration