Results 1 to 5 of 5
Thread: [Q] Need help array
- 03-03-2013, 11:16 PM #1
Member
- Join Date
- Mar 2013
- Posts
- 5
- Rep Power
- 0
[Q] Need help array
Hello I'm new to this forum and I'm looking for a bit of help with an array that won't work for me it just gives me this error and I can't see why
ERROR: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Mid_Exam3.main(Mid_Exam3.java:29)
Here's the code
I can't figure why its going out of bounds cause it takes in the 10 inputs and then prints out the array and then the errorJava Code:import java.util.Scanner; public class Mid_Exam3 { public static void main(String[] args) { Scanner myinput = new Scanner(System.in); final int MAX_SIZE = 10; int [] list = new int [MAX_SIZE]; int average; int check; int i; int sum = 0; int flag = 0; for (i=0; i<MAX_SIZE; i++) { System.out.println("Enter a number and press return"); list[i] = myinput.nextInt(); sum = sum + list[i]; } System.out.println(); for (i=0; i<=MAX_SIZE; i++) { System.out.print(list[i]+" "); } System.out.println(); System.out.println("Average = "+sum/MAX_SIZE); System.out.println("Please enter another number and press return"); check = myinput.nextInt(); for (i=0; i<MAX_SIZE; i++) { if (check == list[i]) { System.out.println(check+" is on the list in postin "+i); flag = flag+1; } else { System.out.println(check+" is not on the list"); } } } }
Any help that you can give me would be great also not looking for somebody to just say change this to this if you could tell me why anything needs to be changed or where I went wrong that would be great thanks in advance hopefully I'll be able to help some other members here
- 03-04-2013, 12:23 AM #2
Member
- Join Date
- Mar 2013
- Posts
- 5
- Rep Power
- 0
Re: [Q] Need help array
Can anybody please help me with this I have an exam tomorrow and I can't figure this one out
-
Re: [Q] Need help array
Do you see any difference between these two for loops?
It's the second loop that is causing the AIOOBE to be thrown.Java Code:for (i=0; i < MAX_SIZE; i++) { // ... } // ... for (i=0; i <= MAX_SIZE; i++) { // ... }
Please don't bump your question only 1 hour after posting it. You may be in a rush, but remember that we are all volunteers, and we will try to help you as soon as we can, but please understand that your question is no more important than any other question here.
- 03-04-2013, 12:50 AM #4
Member
- Join Date
- Mar 2013
- Posts
- 5
- Rep Power
- 0
Re: [Q] Need help array
Thanks didn't see that. Didn't mean the second post to be a bump just wanted to get ans go I could make sure I understood the problem if it was a big one before tomorrow but I'll refrain from doing it again.
Also what is AIOOBE never heard of that
Thanks again
-
Re: [Q] Need help array
Similar Threads
-
[Problem] Enhanced for-loop with 2D arrays (or array in array)
By thewrongsyntax in forum New To JavaReplies: 0Last Post: 10-07-2012, 08:49 PM -
Reading a text file into an Array and spliting the content into another Array
By jtothemax in forum New To JavaReplies: 15Last Post: 05-14-2012, 12:42 PM -
Display Array on another class after extracting from txt file and into array problem
By jonathan920 in forum NetBeansReplies: 0Last Post: 05-12-2011, 07:04 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks