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

G

gbattine

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+" ");
}
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.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);
//out.writeByte
//out.write(" ");
} //write the array of bytes to file
}
}
 
M

Moiristo

gbattine said:
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
<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());
 
G

gbattine

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..
 
M

Moiristo

gbattine said:
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.
 
G

gbattine

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.getBytes());

into

byte[] bytesnew=intest.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........
 
G

gbattine

There is an error....i want say

temp.addElement(intest.getBytes());

into

byte[] bytesnew=intest.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
 
M

Moiristo

gbattine said:
There is an error....i want say

temp.addElement(intest.getBytes());

into

byte[] bytesnew=intest.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


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));
 
G

gbattine

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.......
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top