ArrayIndexOutOfBoundsException: 0 help me....

G

gbattine

Hi guys,
i've developed a java application that give me this error in run-time

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Riga.main(Riga.java:51)

Can you help me to solve it?Thanks...

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Riga{
private static String fileName = "Dato2.txt";
private String geneid=null;
private double[] values=new double[5];
private static int row=0;
private static int numberOfNumericColumns=0;
private static int col=0;

public Riga(String idGene,double[] x ) {
this.geneid=idGene;
this.values=x;
}
public String getgeneid(){
return this.geneid;
}
public void setgeneid(String idGene){
this.geneid=idGene;
}
public double[] getvalues(){
return this.values;
}
public void setvalues(double[] x){
this.values=x;
}
public void id(){
System.out.println("La linea è"+geneid+values);
}


public static void main(String[] args) throws IOException {
FileReader reader=new FileReader(fileName);
BufferedReader br = new BufferedReader(reader);
ArrayList <Riga> rows = new ArrayList <Riga>();
String line = null;
double[] y=new double[(numberOfNumericColumns)];
while ((line = br.readLine()) != null) {
line = line.replace (',', '.');
row++;

StringTokenizer st = new StringTokenizer(line);
numberOfNumericColumns = (st.countTokens()-1);
col=(numberOfNumericColumns+1);
String x=st.nextToken();
for (int i=0;i<numberOfNumericColumns;i++){
y=Double.parseDouble(st.nextToken());
}
Riga z=new Riga(x,y);
rows.add(z);
}

for(int i=0;i<row;i++)
rows.get(i).id();
}
}
 
O

opalpa

When you make array named y the value of numberOfNumericColumns is 0 so
you are making an array that cannot hold any doubles. Make the array
of doubles named y after assigning a value to numberOfNumericColumns
and before loop populatiing y.

Opalinski
(e-mail address removed)
http://www.geocities.com/opalpaweb/
 
E

eNc^

You can also try using a vector

// Start Code
private Vector v;

// in main method;

v = new Vector();
v.add(put the item you want to add here);

//to the value in the first index

v.getElement(0);

// make a for loop to get all the values
// remember to cast or convert types from the vector to the object type
you want if
// necessary.

// End Code


The good thing about a vector is that it always grows so only if you
reference to an element which is out of bounds, will you get an
outOfBoundsException
 
G

gbattine

Thanks guys,
i've solved my problem declaring the array of double few rows
below,after numberOfNumericColumns has been assigned a value and my
application runs correctly.
My problem is now that with an input file so made:

dsad 23,4 5.3 5.6 18,2 33,1
giauisa 27,4 15.3 25.6 8,2 3,1

i have this output

dsad [D@10b62c9
giauisa [D@82ba41

What's the problem in my code?
Can you help me please?
Thanks
 
J

Jussi Piitulainen

gbattine said:
Thanks guys,
i've solved my problem declaring the array of double few rows
below,after numberOfNumericColumns has been assigned a value and my
application runs correctly.
My problem is now that with an input file so made:

dsad 23,4 5.3 5.6 18,2 33,1
giauisa 27,4 15.3 25.6 8,2 3,1

i have this output

dsad [D@10b62c9
giauisa [D@82ba41

What's the problem in my code?
Can you help me please?
Thanks

You get that output when you try to print an array just like that:

class Foo {
public static void main(String [] _) {
double [] xs = new double [] { 3, 1, 14 };
System.out.println("xs " + xs);
}
}

A solution is to build the output string from the elements of the
array in a loop, like so:

StringBuilder sb = new StringBuilder("xs");
for (double x : xs) {
sb.append(' ');
sb.append(x);
}
System.out.println(sb);

If your Java version is old, use StringBuffer and the more general
loop syntax instead.
 
G

gbattine

Thanks too much for your help,but i'm not able to modify my code with
your suggests...
can you help me with code, can you show me how modify it?
Thanks too much for you availability and excuse me for my english and
my inexperience.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top