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

Need help with String method

 
Jere Johnson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
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;}
 
Elouise Kivineva
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic