Reputation: 109
The assignment calls for a user to input 3 radius and 3 height entries which I am to gather in an array and then determine the volume for each. I am stuck on the array. For some reason I get an ArrayIndexOutOfBoundsException
.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
(CylinderTest.java:19)
I get the error at the last (6th, or height of the third entry). I don't understand what I'm doing wrong. I am having difficulty understanding the logic, is my biggest problem.
Here is the CylinderTest (main)
import javax.swing.*;
//Driver class
public class CylinderTest
{
public static void main(String[] args)
{
Cylinder[] volume = new Cylinder[3];
for (int counter = 0; counter < 6; counter++)
{
double radius = Double.parseDouble(JOptionPane
.showInputDialog("Enter the radius"));
double height = Double.parseDouble(JOptionPane
.showInputDialog("Enter the height"));
volume[counter++] = new Cylinder(radius, height);
}
String display = "Radius\tHeight\n";
for (Cylinder i : volume)
{
if (i != null)
display += i.toString() + "\n";
}
JOptionPane.showMessageDialog(null, display);
}
}
and here is the Cylinder class
public class Cylinder
{
// variables
public static final double PI = 3.14159;
private double radius, height, volume;
// constructor
public Cylinder(double radius, double height)
{
this.radius = radius;
this.height = height;
}
// default constructor
public Cylinder()
{this(0, 0);}
// accessors and mutators (getters and setters)
public double getRadius()
{return radius;}
public void setRadius(double radius)
{this.radius = radius;}
public double getHeight()
{return height;}
public void setHeight(double height)
{this.height = height;}
public double getVolume()
{return volume;}
public void setVolume(double volume)
{this.volume = volume;}
// Volume method to compute the volume of the cylinder
public double volume()
{return PI * radius * radius * height;}
public String toString()
{return volume + "\t" + radius + "\t" + height; }
}
Upvotes: 2
Views: 501
Reputation: 85
Are you sure with this?
Cylinder[] volume = new Cylinder[3];
for (int counter = 0; counter < 6; counter++)
{
double radius = Double.parseDouble(JOptionPane
.showInputDialog("Enter the radius"));
double height = Double.parseDouble(JOptionPane
.showInputDialog("Enter the height"));
volume[counter++] = new Cylinder(radius, height);
//counter will count up to 5 (Array out of Bounds exception for sure..)
//also: why are you incrementing counter by yourself?
}
Upvotes: -1
Reputation: 62062
Your Cylinder
array has only 3 elements. Cylinder[] volume = new Cylinder[3];
Your for loop
is trying to access elements after element 2 within this array. Those elements do not exist.
Upvotes: 2
Reputation: 11486
First of all, your array has been declared with a size of three. But, in your for loop, you access at the very least 6 elements of the array. So you will have increase the size of your array to at least 6. And you should change the code:
volume[counter++] = new Cylinder(radius, height);
To
volume[counter] = new Cylinder(radius, height);
Upvotes: 1
Reputation: 1556
You are declaring a 3 size array of Cylinder and you are trying to read the 6 of them.
Cylinder[] volume = new Cylinder[3]; // 3 size array
for (int counter = 0; counter < 6; counter++) // loop 6 times
{
double radius = Double.parseDouble(JOptionPane
.showInputDialog("Enter the radius"));
double height = Double.parseDouble(JOptionPane
.showInputDialog("Enter the height"));
volume[counter++] = new Cylinder(radius, height); // read 0, 1, 2, 3, 4, 5...
}
That should be:
Cylinder[] volume = new Cylinder[3]; // 3 size array
for (int counter = 0; counter < volume.length; counter++) // loop 6 times
{
double radius = Double.parseDouble(JOptionPane
.showInputDialog("Enter the radius"));
double height = Double.parseDouble(JOptionPane
.showInputDialog("Enter the height"));
volume[counter] = new Cylinder(radius, height);
}
Note the volume.length instead of 6 and the ++ removed in volume[counter++]
Upvotes: 1
Reputation: 13351
Your array has size 3, determined by this line:
Cylinder[] volume = new Cylinder[3];
Then you loop from 0 to 5 here:
for (int counter = 0; counter < 6; counter++)
And then you try to access one of those indexes here:
volume[counter++] = new Cylinder(radius, height);
As the array has length 3, it only has indexes 0, 1 and 2. And yet you try to access indexes higher than two.
As a side note, I suggest you change the statement to volume[counter] = new Cylinder(radius, height);
otherwise you are increasing the for-loop variable counter
twice in each iteration.
A good practice when looping through indexes in an array is to use the array's length in the condition:
for (int counter = 0; counter < volume.length; counter++)
This will make sure that it only iterates through the indexes that exist in the array, no matter how big or small it is.
Upvotes: 1