Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

ArrayIndexOutOfBounds help!

 
Greenhorn
Posts: 29
  • 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();

}
}
 
Ranch Hand
Posts: 4632
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, i think that should help.
    Bookmark Topic Watch Topic
  • New Topic