The Artima Developer Community
Sponsored Link

Java Answers Forum
can a method return more than one value

1 reply on 1 page. Most recent reply: Nov 15, 2002 8:00 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
George

Posts: 10
Nickname: swiftcrash
Registered: Nov, 2002

can a method return more than one value Posted: Nov 15, 2002 7:11 PM
Reply to this message Reply
Advertisement
is there a way where a method would take a string
and return more that one string
and how is the syntax in

return one,two;


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: can a method return more than one value Posted: Nov 15, 2002 8:00 PM
Reply to this message Reply
you can only return one "thing" which can be either or primitive data type such as: int, char, float, double
or an array of primitive types, or an Object or an array of Objects or void which is nothing.

You can create you own class of objects to hold one or more String objects and then return the single object containing the Strings processed by your method.

It would be more straightforward in your case to just return an array of Strings such as in the following snippet:
public String[] process(String s){
    String[] results = new String[2];
    results[0] = s.substring(0, s.indexOf(":")); 1st part
    results[1] = s.substring(s.indexOf(":")); // 2nd part
    return results;
}
 

Flat View: This topic has 1 reply on 1 page
Topic: how do u add two arrays Previous Topic   Next Topic Topic: how do u add two arrays

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use