Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 'Return'
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
xHoly-Divinity




PostPosted: Tue Nov 29, 2005 5:23 pm   Post subject: 'Return'

code:

public String view (String firstName)
    {
        for (int i = 0 ; i < entry ; i++)
        {
            if (firstName.equals (firstNames [i]))
            {
                return (lastNames [i]);
                return (phones [i]);
                return (emails [i]);
                return (provinces [i]);
                return (cities [i]);
                return (streets [i]);
                return (houses [i]);
                return (postalCodes [i]);
            }
        }
        return "";
    }


Is it possible to return all those values as opposed to just returning one?
Sponsor
Sponsor
Sponsor
sponsor
JackTruong




PostPosted: Tue Nov 29, 2005 5:33 pm   Post subject: (No subject)

Make the return type an array;

Java:
public String[] view (String firstName)


And put all the values of the address in an array.

Java:
String address[] = {lastNames [i], phones [i], emails [i], provinces [i], cities [i], streets [i], houses [i], postalCodes [i]};
return address;
wtd




PostPosted: Tue Nov 29, 2005 5:33 pm   Post subject: (No subject)

I can see from this that you ave bigger design problems, and solving the design problem solves the question you're asking.

You're creating several arrays and having them side by side storing different piece of data. Instead of doing this, we should put all of that related data into a cohesive unit. In Java speak, an "object".

Each objedct stores a first name, a last name, a phone number, an address, etc. Then we have an array of these objects. Returning all of that data is as easy as returning the object.
xHoly-Divinity




PostPosted: Tue Nov 29, 2005 5:47 pm   Post subject: (No subject)

Truong's way works. Thanks =P
wtd




PostPosted: Tue Nov 29, 2005 5:55 pm   Post subject: (No subject)

Maybe, but it's just letting you get away with not knowing how to properly design a program.
xHoly-Divinity




PostPosted: Tue Nov 29, 2005 6:07 pm   Post subject: (No subject)

Well can you explain a little more clearly how to do what you were saying
wtd




PostPosted: Tue Nov 29, 2005 6:27 pm   Post subject: (No subject)

The problem with your existing solution is that you're storing each part of one "person" separately. This allows those parts to be mismatched, and it makes grouping them together tedious.

Rather, proper design would be to keep all of these pieces of data about one "person" grouped together in a single object.

For instance, instead of matching arrays of first names and last names...

Java:
class Name {
   private String first, last;

   public Name(String first, String last) {
      this.first = first;
      this.last = last;
   }

   public String getFirst() { return first; }
   public String getLast() { return last; }
}
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: