Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

ArrayIndexOutOfBoundsException: 0  help me....

Thread view: 
gbattine - 13 Jun 2006 23:04 GMT
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[i]=Double.parseDouble(st.nextToken());
     }
     Riga z=new Riga(x,y);
     rows.add(z);
   }
       
        for(int i=0;i<row;i++)
            rows.get(i).id();
    }
}
opalpa@gmail.com opalinski from opalpaweb - 13 Jun 2006 23:13 GMT
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
opalpa@gmail.com
http://www.geocities.com/opalpaweb/

> Hi guys,
> i've developed a java application that give me this error in run-time
[quoted text clipped - 63 lines]
>     }
> }
eNc^ - 14 Jun 2006 00:04 GMT
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
gbattine - 14 Jun 2006 09:21 GMT
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
Jussi Piitulainen - 14 Jun 2006 09:57 GMT
> Thanks guys,
> i've solved my problem declaring the array of double few rows
[quoted text clipped - 13 lines]
> 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.
gbattine - 14 Jun 2006 10:19 GMT
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.
gbattine - 14 Jun 2006 12:08 GMT
Thanks guys....
i've solved my question.....
thank you for your helps..


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.