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.

Exception in thread "main" java.lang.ClassCastException: [B

Thread view: 
gbattine - 21 Jun 2006 14:23 GMT
Hi guys,
i need a disperate help.....i'm crazying on this problem and i don't
find an exit. I know this exception mean a bad casting...but can you
help me to know how solve it?
Without your help i'm not able to go on...
please help me....

Exception in thread "main" java.lang.ClassCastException: [B
at giu.princ.getBytes(princ.java:142)
at giu.princ.main(princ.java:158)

And my function is

package giu;
import java.io.BufferedReader;
import java.io.*;
import java.io.IOException;
import java.util.*;

public class princ {

    private static String fileName = "Dato2.txt";
    private static String file = "ris.txt";
    private String geneid=null;
    private static int row=0;
    private static int numberOfNumericColumns=0;
    private static int col=0;
    Double[] values=new Double[numberOfNumericColumns];
    String[]intest=null;
    private ArrayList rows = new ArrayList();
    Head h;
    Riga r;

    public boolean insRighe(Riga nuovo){
          return rows.add(nuovo);
    }

    public List stampaRows(){
        return rows;}

    public Head stampaHead(){
        return h;}

    public void carica()throws IOException{

        FileReader reader=new FileReader(fileName);
        BufferedReader br = new BufferedReader(reader);

        String line = null;

        while ((line = br.readLine()) != null) {

            line = line.replace (',', '.');

            StringTokenizer st = new StringTokenizer(line);
            numberOfNumericColumns = (st.countTokens()-1);
           col=(numberOfNumericColumns+1);
           //se siamo nella prima riga(contatore segna 0)
           if(row==0){
               intest=new String[col];
               int j=0;
               while(st.hasMoreTokens()){
                     intest[j]=(st.nextToken().trim());
                     j++;
               }
               /*for(int w=0;w<=intest.length-1;w++){
                      System.out.println(w+" "+intest[w]);
                }*/

               //STAMPA LA RIGA HEAD ALL'INIZIO DELL'OUTPUT
               h=new Head(intest);//crei l'oggetto head
               String []qa=h.getHvalues();
                String asd="";
                for(int i=0;i<=qa.length-1;i++){
                    asd=asd.concat(qa[i]+" ");
                }
                System.out.println("head "+asd);//stampo contenuto dell' head
                row=1;
            }//fine if

           else
           {
               Double[] values=new Double[numberOfNumericColumns];
                int z=0;
                geneid=st.nextToken();
                while (st.hasMoreTokens()) {
                    String app=st.nextToken();
                   values[z]=Double.valueOf(app);
                   z++;
                }
                r=new Riga(geneid,values); //crei l'oggetto riga

                System.out.println("riga");
                System.out.println(r.getgeneid());

                values=r.getvalues();
                for(int e=0;e<=values.length-1;e++){
                    System.out.println(values[e]);
                }
                insRighe(r); //aggiungi
            }
           row++;
        }
    }

    public byte[] getBytes(){
        byte middlerow=' ';
        byte endrow=';';
        byte[] data;
        Vector temp=new Vector(100000);
        int i=0;
        String g=null;
        Riga r;
        Double val[];
        while(i<intest.length){
            temp.addElement(intest[i].getBytes());
            temp.addElement(Byte.valueOf(middlerow));
            i++;
        }
        temp.addElement(Byte.valueOf(endrow));
        System.out.println("Intestazione convertita in byte");

        for(int l=0;l<rows.size()-1;l++){
            r=(Riga)rows.get(l);
            g=r.getgeneid();
           temp.addElement(g.getBytes());
           temp.addElement(Byte.valueOf(middlerow));
           val=r.getvalues();

           byte[] tempByte1;
           for(int e=0;e<=val.length-1;e++){
                //Returns a string representation of the double argument.
                tempByte1 = Double.toString(val[e]).getBytes();
                System.out.println("la soluzione e"+tempByte1);
                for (int j = 0; j >< tempByte1.length; j++) {
                    temp.addElement(Byte.valueOf(tempByte1[j]));

                    temp.addElement(Byte.valueOf(middlerow));

                }
                temp.addElement(Byte.valueOf(endrow));
                System.out.println("la soluzione 4e"+tempByte1);
            }
        }
        data=new byte[temp.size()];
        System.out.println("la soluzione 2e"+data);
        for (int t=0;t<temp.size()-1;t++)
            data[t]=(((Byte)temp.elementAt(t)).byteValue());
        System.out.println("la soluzione nuova");
        System.out.println(temp);
        /*for (int k=0;k<=temp.size()-1;k++){
            System.out.println(k+ " temp "+temp.get(k));
        }*/

        return data;

    }
    public static void main(String[] args) throws IOException {
        byte x[];
        String totale=null;
        princ p=new princ();
        p.carica();
        System.out.println("Dati caricati");
        x=p.getBytes();
        System.out.println("Byte convertiti");
        BufferedWriter out = new BufferedWriter(new FileWriter(file));; //
define the output stream
        for(int i=0;i<=x.length;i++){
            System.out.println(x[i]);
            //out.writeByte
            //out.write(" ");
        }     //write the array of bytes to file
}
}
Moiristo - 21 Jun 2006 14:27 GMT
> Hi guys,
> i need a disperate help.....i'm crazying on this problem and i don't
[quoted text clipped - 8 lines]
>
> And my function is
<cut code>

'[B' is the string representation of the data you are casting. It means
that the data is in fact a byte array, not a byte.

> data[t]=(((Byte)temp.elementAt(t)).byteValue());
gbattine - 21 Jun 2006 14:39 GMT
Thanks Moiristo for your reply....but i don't understand well what's
the problem.
Can you help me explaing it and giving me a solution,if you know it?
Thanks very much..
Moiristo - 21 Jun 2006 14:59 GMT
> Thanks Moiristo for your reply....but i don't understand well what's
> the problem.
> Can you help me explaing it and giving me a solution,if you know it?
> Thanks very much..

In rules 109, 119 and 129, you include a bytearray (the result of
getBytes()) to your temp Vector. An index of a Vector is not a start
position, it refers to an object, which can be a whole array. Therefore,
instead of adding the bytearray from String.getBytes(), you need to add
every byte in the resulting bytearray separately to the Vector. This can
be done by using the function Vector.addAll(<collection>). And AFAIK,
you don't have to use the addElement method for adding a byte, you can
just use the method Vector.add(<object>). Furthermore, I see no real
benefit in giving the Vector an inital capacity; a Vector grows
automatically.
gbattine - 21 Jun 2006 15:57 GMT
Thanks!!!
I'm understanding.......
but i'm not expert in java,can you help me?
If i've understood i've to modify

temp.addElement(intest[i].getBytes());

into

byte[] bytesnew=intest[i].getBytes();
temp.addAll(bytes2);

but the addAll function return a boolean?
How can i do in this specific example?
Can you help me with code?
Thanks........
gbattine - 21 Jun 2006 16:00 GMT
There is an error....i want say

temp.addElement(intest[i].getBytes());

into

byte[] bytesnew=intest[i].getBytes();
temp.addAll(bytesnew);

And for

temp.addElement(g.getBytes());
and
tempByte1 = Double.toString(val[e]).getBytes();

can you help me?
I'm stopped and i need to go on..............thanks too much
gbattine - 21 Jun 2006 19:12 GMT
I try and try but nothing.........can someone help me?
Moiristo - 21 Jun 2006 19:31 GMT
> There is an error....i want say
>
[quoted text clipped - 13 lines]
> can you help me?
> I'm stopped and i need to go on..............thanks too much

I'm sorry, but addAll needs a Collection, you cannot add an array. It's
better to create a for loop like this:

foreach(byte b : bytesnew) temp.add(new Byte(b));
gbattine - 22 Jun 2006 09:07 GMT
Thanks Moiristo...
i've solved my exception but i'm not sure the conversion file to array
of byte works fine.
I need to develop inverse function, i've the array of byte and i want
to rebuild the original format.
How can i do?
I know each string can be coded into more byte like the double...i only
know that whitespace(in byte 32) separates different values and ;(in
byte:59) separate different rows.
I think for the first load the array of double into a string,what's the
code?

public String(byte[] bytes)?

Can you help me with initial code to separate different rows basing on
;(59)?
Thanks very much for your availability.......


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.