how to insert int type array value to String type value?
publicclass wood
{
publicstaticvoid main (String args[])
{
int a[]={1};
String b[]={" "};
b=a[0];
}
}
how can I do it?I know it is int type, but how to insert a[0] to b[0]?
publicclass wood
{
publicstaticvoid main (String args[])
{
int a[]={1};
String b[]={" "};
b=a[0];
}
}
how can I do it?I know it is int type, but how to insert a[0] to b[0]?
If you want to replace b[0] with the String value of a[0] then use valueOf or new as follows:
b[0] = new String(a[0]);
// or
b[0] = String.valueOf(a[0]);
If you want to insert it (i.e. the current b[0] becomes b[1]) then you need to create a new array with a length 1 longer than the previous, assign the first value as above, then use System.arraycopy to copy the rest of the original values. But I believe you meant replace, right? ;-)
> If you want to replace b[0] with the String value of
> a[0] then use valueOf or new as follows:
> > b[0] = new String(a[0]);
> // or
> b[0] = String.valueOf(a[0]);
>
> If you want to insert it (i.e. the current
> b[0] becomes b[1]) then you need to create a new
> array with a length 1 longer than the previous,
> assign the first value as above, then use
> System.arraycopy to copy the rest of the original
> values. But I believe you meant replace, right? ;-)
:-< compile error
C:\Documents and Settings\a1\Desktop\wood.java:7: cannot find symbol
symbol : constructor String(int)
location: class java.lang.String
b[0] = new String(a[0]);
> Sorry new doesn't take ints. Duh! Just use the
> valueOf.
I see... But I believe that can be use in the method but not...such as
class a{
int aInt;
int bInt;
public a(int aInt, int bInt){
this.aInt=aInt;
this.bInt=bInt;
}
}
class b{
a bArray[];
int value=0;
public a insert(a aArray[], int value){
String bString[];
bString= String.valueOf(a[value]);
}
}
Can it be possible to change a[value] to bString[]?
Message was edited by:
PaT
Message was edited by:
PaT
> You need to convert each element. But as paulcw
> asked, what exactly is it you are trying to
> accomplish?
the idea is same as above of my new uploading code
and I want change int type to string and return string...
It seem can not be run because there are two int type
I don't understand this statement.
What I want to know is
1) why are you trying to convert an int[] to a String[]
2) are you trying to convert from a int[] to String[] or from an int[] to a String (i.e. treating the ints as chars or even simply concatenating there String values to a single String) and , once again why
There is probably a better way to accomplish your goals, but without cleary stating your goals we cannot help you. And as far as there being multiple int values, so what, the code snippet I posted would work for a hundred int values, you would just need to make the String[] long enough and loop through the values, you can't just simply do it all at once (unless of course, they are to be treated as char values, in which case you can probably cast your int[] to a char[] and use new String).