The Artima Developer Community
Sponsored Link

Java Answers Forum
Methodical Dilemma

2 replies on 1 page. Most recent reply: Mar 30, 2002 10:12 PM by Matt Gerrans

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 2 replies on 1 page
SinTahLyZing

Posts: 2
Nickname: impuls3
Registered: Mar, 2002

Methodical Dilemma Posted: Mar 30, 2002 3:41 PM
Reply to this message Reply
Advertisement
I was just wondering if anyone could point me in the right direction ..

How do you return 3 values from a method? The go method is provided, but then you have to write a method called getInt to return the 3 values (num1, num2, num3). I can get my method to return 1 value, but not all 3 ..

public void go() {
int num1 = getInt("first");
int num2 = getInt("second");
int num3 = getInt("third");
}

public int getInt(String first){
System.out.print("Enter your " + first + " number: ");
int num1 = Integer.parseInt(readInput());
return num1;
)

I tried putting in String second, String third in the parenthesis of the getInt method, but it regurgitates some weird pig-latin-type error msg .. So my question is, how do i go about modifying this source code to incorporate all 3 parameters and return the 3 values.. !! Help anyone? :\


Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: Methodical Dilemma Posted: Mar 30, 2002 8:52 PM
Reply to this message Reply
return them in an array of integers... :)

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Methodical Dilemma Posted: Mar 30, 2002 10:12 PM
Reply to this message Reply
As suggested above, returning an array of three ints does the trick.

It may be that returning an object is even better. For instance, if what you were returning is a point in 3-D space, then you'd want to return a Point3d (or more precisely, javax.vecmath.Point3d) object. This will be simpler to work with (you won't have to unpack the array into whatever form it really needs) and it will give you much more readible code. It will also help you to think more about your design and improve your object-oriented thinking. This will then contribute to you garnering the respect and admiration of your peers, which will, of course, inevitably lead to the formation of a fan club in your honor, an Oscar (don't forget to mention me in your acceptance speech), your very own star on Hollywood Boulevard, a Pulitzer and eventually a Nobel Prize.

Incidentally, one of cool features of the Python (http://www.python.org) programming language is that you can return more than one object or value, or at least appear to. You can do something like "return x, y, z" and it works fine. In reality, you are really only returning one thing, as the three things are wrapped into a tuple, but the effect is that it works like you have multiple return values, since you can also assign multiple variables when you call the method. So you might have something like this:
def Foo( x, y, z ):
    # Do some incredibly interesting calculations.
    return x, y, z
 
# Then, later use it like this:
a, b, c = Foo( d, e, f )

Flat View: This topic has 2 replies on 1 page
Topic: xml Previous Topic   Next Topic Topic: HELP please!

Sponsored Links



Google
  Web Artima.com   

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