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]?

[667 byte] By [PaTa] at [2007-11-27 1:43:00]
# 1

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

masijade.a at 2007-7-12 1:00:26 > top of Java-index,Java Essentials,Java Programming...
# 2
Java is strongly typed. If you declare a variable to have a int type, you can't assign a String to it, or vice-versa.What are you actually trying to accomplish? Why are you hoping to do this thing?
paulcwa at 2007-7-12 1:00:26 > top of Java-index,Java Essentials,Java Programming...
# 3
I want insert value 1 from a[] into b[],then I use System.out.print("b[]:"+Arrays.toString(b));the result is expected to:b[]: [1]
PaTa at 2007-7-12 1:00:26 > top of Java-index,Java Essentials,Java Programming...
# 4

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

PaTa at 2007-7-12 1:00:26 > top of Java-index,Java Essentials,Java Programming...
# 5
Sorry new doesn't take ints. Duh! Just use the valueOf.
masijade.a at 2007-7-12 1:00:26 > top of Java-index,Java Essentials,Java Programming...
# 6

> 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

PaTa at 2007-7-12 1:00:26 > top of Java-index,Java Essentials,Java Programming...
# 7
You need to convert each element. But as paulcw asked, what exactly is it you are trying to accomplish?
masijade.a at 2007-7-12 1:00:26 > top of Java-index,Java Essentials,Java Programming...
# 8

> 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

PaTa at 2007-7-12 1:00:26 > top of Java-index,Java Essentials,Java Programming...
# 9

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

masijade.a at 2007-7-12 1:00:26 > top of Java-index,Java Essentials,Java Programming...
# 10
public a insert(a aArray[], int value){String bString[];bString= String.valueOf(a[value]);return bString;}}I want to convert aArray[] to bString[]...
PaTa at 2007-7-12 1:00:26 > top of Java-index,Java Essentials,Java Programming...
# 11
Then, as I said, make your String array the same length as your int array and loop through them.
masijade.a at 2007-7-12 1:00:26 > top of Java-index,Java Essentials,Java Programming...
# 12
I see that, thx for help. You are so helpful and kind :->
PaTa at 2007-7-12 1:00:26 > top of Java-index,Java Essentials,Java Programming...