Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Need help with String method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with String method" Watch "Need help with String method" New topic

Need help with String method

Jere Johnson

Joined: Mar 29, 2003
Posts: 28
I would like to know how to return every component which is a string of an array with a method. This seems not to be working. Does anybody know how to write the code for this? Thanks.

public String toString(String [] array){
String message;
for (int i=0; i < array.length; i++)
message = array[i];
return message;}

Diapers are the best invention
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
You need to do a better job of explaining what you want to do.
Are you trying to write a method that will return each and every string contained in an array of strings? If so, you need to realize that a method only returns once. If it is a method that returns a string you only get one string.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Look at your code. What will "message" contain after you get out of the 'for' loop? Well it can only contain one value, so I guess it would be the last value in the array, since you assigning every value in the array to this variable.
And ya to everything Elouise said.

Please ignore post, I have no idea what I am talking about.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
It also looks like you are posting similar questions in separate threads. Please keep the confusion limited by keeping our conversation to a single thread.
Thank you.

Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: Need help with String method
It's not a secret anymore!