aspose file tools*
The moose likes Beginning Java and the fly likes ArrayIndexOutOfBounds help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayIndexOutOfBounds help!" Watch "ArrayIndexOutOfBounds help!" New topic
Author

ArrayIndexOutOfBounds help!

unyime inok
Greenhorn

Joined: Oct 13, 2004
Posts: 29
import java.io.*;
public class Array
{
private int [] xIntegers;
private int numOfValues;
private double average;
public final int sizeOfArray = 100;

public Array()
{
xIntegers = new int [0];
numOfValues = 0;
}

public Array(int [] xIntegers, int sizeOfArray, int numOfValues)
{
xIntegers = new int[sizeOfArray];
numOfValues = 0;
}

public void readValues()throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inString = new String();

System.out.println("Enter the number of values in the array");
inString = br.readLine();
numOfValues = Integer.parseInt(inString);

for(int i = 1; i <numOfValues; i++)
{
System.out.println("Please enter the number in the array");
inString = br.readLine();
xIntegers[i] = Integer.parseInt(inString);


average = 0.0;
average = average + xIntegers[i];
average = average/numOfValues;
System.out.println("The Average is" + "" + average);
}




}


public void writeValues()
{
for(int i= 1; i<numOfValues; i++)
{
System.out.println("The values in the array are" + xIntegers[i]);
}
}

public int getNumOfValues()
{
return numOfValues;
}

public double getAverage()
{
return average;
}

public double calStandardDev(int [] xIntegers, double average)
{
double sd = 0.0;
double diff;
double sum = 0.0;
for (int i = 1; i < xIntegers.length; ++i)
{
diff = xIntegers[i] - average;
sd = sum + diff * diff;
}
sd = Math.sqrt(sd/(xIntegers.length));
System.out.println("The Standard Deviation is" + "" + sd);
return sd;
}



}









//unyime i
import java.io.*;
public class TestArray
{

/**THE MAIN PROGRAM STARTS HERE, PROMPTS THE USER FOR INPUT
AND ALLOWS THE USER TO SET THE SIZE OF THE ARRAY, CALLS THE
APPROPRIATE METHODS TO DO THE COMPUTATIONS AND PRINTS THE
RESULT **/

public static void main (String []args) throws IOException
{
Array ta = new Array();
ta.readValues();
ta.writeValues();

}
}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
In the constructor public Array() you are setting the array
xIntegers = new int [0]//has no elements

you need to initialize it after you have obtained the size from the user
something like this

System.out.println("Enter the number of values in the array");
inString = br.readLine();
numOfValues = Integer.parseInt(inString);
xIntegers = new int [numOfValues];//<-----------
for(int i = 0; i <numOfValues; i++)//<-----------changed i = 1; to i = 0
{
unyime inok
Greenhorn

Joined: Oct 13, 2004
Posts: 29
Thanks a lot, i think that should help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayIndexOutOfBounds help!