File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayIndexOutOfBounds help!

 
unyime inok
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, i think that should help.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic