wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Can we convert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can we convert "Array of Strings" to a "String"??" Watch "Can we convert "Array of Strings" to a "String"??" New topic
Author

Can we convert "Array of Strings" to a "String"??

Shashi Kala
Ranch Hand

Joined: Jan 27, 2008
Posts: 46
Hi Everybody,

I am having a method which returns Array of string. Suppose..

public String[] method()
{
.....
.....
}
Assume that this method is written in a Class.
Some where, I want to call this method and store into a variable like below.

String[] str = obj.method();

Now I want to pass this variable 'str' as a parameter of another method. (eg: callingmethod(str.toString()) )
Suppose that this method is..

void callingmethod(String str)
{
.....
.....
}

Because the above method takes the parameter only string, I converted used str.toString(). Can I use like this..? If not How can I do the same in different way??
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

what do you really want to do? if you call toSring on a String array, you'll get something like "[Ljava.lang.String;@3e25a5" - this is probably not what you want.

are you trying to build one long string with all the strings in the array concatenated? are you trying to pass the actual array in?

technically, you CAN do this, but I doubt it will give you what you want. it's just not clear what you DO want.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jigar Gandhi
Greenhorn

Joined: May 13, 2008
Posts: 20
In your code snippet "str" is a array of string and not a string
if you want to pass the String array inside the function you can use
public functionname(String[] a)
{
//method body
}

if you want to convert your string array into a single array
use the following code snippet in your program

String tempstr="";
for(int i=0;i<str.length();i++) tempstr+=str[i];
now you can pass temstr in the way you have shown in your code snippet
thanxs
JIGAR R GANDHI
jigarr.gandhi@gmail.com
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Calling toString() on an array will not give you a string that contains the items in your array. It will use the default toString method from the Object class and will give you something to effect of:

[Ljava.lang.String;@9304b1

where the last number is the array's hashcode in memory.

There is a class named "Arrays" that has a static toString method that can take an array. So



will put a value of
[one, two, three]
into the asString variable (including the square brackets). Ultimately it calls the toString() method on each object in the array. So it works well for an array of Strings, an array or any primitives, or an array of any objects that override the toString() method.

Generally doing something like this is only useful for outputting the value of an array in a log or to the screen. By doing this you lose the individual values. While you certainly can use some String manipulation to get the individual values back, that is potentially a lot of work for nothing. Is there a reason why the "callingmethod" can only take a String? Is it a method you wrote, or a method from something else you need to call? Would it be better to call the method for each value in the array? I suggest you post a bit more about what it is you are trying to do and we can help suggest a better alternative.
[ June 02, 2008: Message edited by: Mark Vedder ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
You could take a StringBuilder and go through a for-each loop and append each member to the StringBuilder, but as people have told you, Why?
Shashi Kala
Ranch Hand

Joined: Jan 27, 2008
Posts: 46
Thanks all for quick response..!

So..as all of you told, toString() returns "[Ljava.lang.String;@3e25a5". Then this is not what I want.

Hi Mark,

As You asked me, the callingmethod() is written by some body and its purpose is to output the values in log. So, Now I can use Arrays.toString() method right?
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Originally posted by Shashi Kala:
...As You asked me, the callingmethod() is written by some body and its purpose is to output the values in log. So, Now I can use Arrays.toString() method right?

yes, as far as you are OK with [one, two, three] this output.
Shashi Kala
Ranch Hand

Joined: Jan 27, 2008
Posts: 46
Thanks every body for your help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we convert "Array of Strings" to a "String"??
 
Similar Threads
Number of String objects
Is it NullPointerException??
Question from Just Java 2
Once more about Array
UnInitialized Strings