Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Shashi Kala
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jigar Gandhi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks every body for your help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic