473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

array out of bounds exception

1
Everytime i input a number it gives me the array out of bounds Eception error : 3 and hihglights the product = product * A{i} bit i would be grateful for any help.

public class Accumulator {
private int[] A;

public Accumulator(int[] X) {
A= new int[X.length];
for (int i=0; i<X.length; i++)
A[i] = X[i];
}

public int prodA(int m) {

int product= 1;

for (int i=0; i<A.length+1; i++)
{

product = product * A[i];
}

if(product<=m)
{
return product;
}

if(product>m)
{
return m;
}

if (product == 0)
{
return 0;
}

return 0;



}

}
Aug 15 '12 #1
1 2403
chaarmann
785 Expert 512MB
look at "A.length+1" in your source code. You have to correct that. Why?

Assume, the length of A is 3.
So what is the value of the index i in the last iteration of your loop?
(Yes, it's 3!)
But you can only access A[0], A[1] and A[2]. (3 elements). and it crashes with an index-out-of-bounds-exception when you try to acces A[3].
Sep 12 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: agascoig | last post by:
I am just starting to learn Java J2RE 5.0. I coded the matrix library below and found that the array accessing is really slow since each access is checked: ...
1
by: Joshua Hale | last post by:
Any suggestions for a debugging tool to help find out of bounds writes/reads to arrays in a C++ program on a Linux platform? I tried valgrind, and it found some accesses of uninitialized values,...
4
by: William McIlroy | last post by:
Array Bounds Exception inside system.xml.dll. Test data is a dozen GB (available for the asking on CD). Source code follows. Call into system.xml.dll happens at the while statement.. using...
6
by: Flip | last post by:
I'm reading the O'Reilly's Progamming C# book and I have a question about array bounds checking. On page 174, near the top, they show an example where c# does indeed to array bounds checking cause...
1
by: noleander | last post by:
Hey. I'm new to Visual C++. I've got a large application, and I need to make sure all array accesses are within bounds. Back on Unix we had a tool called Purify that detected array-out-of-bounds...
4
by: Dave Rahardja | last post by:
I have the following program that uses an array of chars to simulate a bit set: --------- // An out-of-bounds exception class BoundsException {}; template <int bits = 1> class Bitset
6
by: Paul van Brenk | last post by:
When you run the Shuffle method often enough it will throw exception. And I can't figure out why. Anybody? Paul van Brenk the code: static void Shuffle(){ int ints = { 1, 2, 3, 4, 5, 6,...
3
by: writebrent | last post by:
I wrote a little interface for users to post data to a website. On their local machines, it produces CSV from an Excel spreadsheet, then posts it to the site. In some cases, the CSV will contain...
26
by: Adam Warner | last post by:
Hi all, One cannot return a pointer to an array type in C because C has no first class array types. But one can return a pointer to a struct containing an incomplete array via the illegal but...
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.