469,330 Members | 1,379 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

How to return 2 values

How can i return 2 values in below code:

I want to return both x and y. How can I do that?

public boolean populateTab()
{
int tabIndex = _tabPanel.getSelectedIndex();
switch (tabIndex)
{
case BulletinConstants.INPUT_TAB_INDEX:
_bulletinItemTab.init(null);
x = true;
y = false;
Log.enter(getClass(),"Tab moved to Input");
break;
case BulletinConstants.RELAY_TAB_INDEX:
_relayTabAdapter.setLogger(getLogger());
_bulletinRelayTab.init();
Log.enter(getClass(),"Tab moved to Relay");
y = true;
x = false;
break;
default:
break;
}
return x;// I want to return both x and y;
}
Nov 27 '07 #1
5 31804
r035198x
13,262 8TB
How can i return 2 values in below code:

I want to return both x and y. How can I do that?

public boolean populateTab()
{
int tabIndex = _tabPanel.getSelectedIndex();
switch (tabIndex)
{
case BulletinConstants.INPUT_TAB_INDEX:
_bulletinItemTab.init(null);
x = true;
y = false;
Log.enter(getClass(),"Tab moved to Input");
break;
case BulletinConstants.RELAY_TAB_INDEX:
_relayTabAdapter.setLogger(getLogger());
_bulletinRelayTab.init();
Log.enter(getClass(),"Tab moved to Relay");
y = true;
x = false;
break;
default:
break;
}
return x;// I want to return both x and y;
}
1.) Use code tags when posting code
2.) Methods can only return one value in Java. You can however return an array or a composite object e.g you could define a class called TwoBooleans which stores two booleans and then return that instead.
Nov 27 '07 #2
JosAH
11,448 Expert 8TB
Why not 'steal' from C++ what they've done correct? I sometimes use the following
little helper class for the purpose of 'bundling' more than one value:

Expand|Select|Wrap|Line Numbers
  1. public class Pair<T, U> {
  2.  
  3.     public final T t;
  4.     public final U u;
  5.  
  6.     public Pair(T t, U u) {
  7.  
  8.         this.t= t;
  9.         this.u= u;
  10.     }
  11. }
  12.  
We could discuss whether or not both elements should be final; I think they
should be final.

kind regards,

Jos
Nov 27 '07 #3
i use something like this:
Expand|Select|Wrap|Line Numbers
  1. public static Object[] wharever() {
  2.         Object object[] = new Object[3];
  3.         int x = 0;
  4.         String y = "0";
  5.         Boolean z=true;
  6.         object[0] = x;
  7.         object[1] = y;
  8.         object[2]=z;
  9.         return object;
  10.     }
  11.  
  12.     public static void main(String... args) {
  13.         Object object[] = wharever();
  14.         int y = Integer.parseInt( object[1].toString());
  15.         int x =  Integer.parseInt( object[2].toString());
  16.         boolean z=Boolean.parseBoolean(object[3].toString());
  17.     }
  18.  
You can return an Object array and cast to any type you want.
Feb 16 '09 #4
JosAH
11,448 Expert 8TB
@java2986
Please compile and run/test your code before you post it; there's an ArrayIndexOutOfBoundsException in your code.

kind regards,

Jos
Feb 16 '09 #5
Sorry i modified the the method but din't do it to the main

Expand|Select|Wrap|Line Numbers
  1. public static Object[] wharever() {
  2. Object object[] = new Object[3];
  3. int x = 0;
  4. String y = "1";
  5. Boolean z=true;
  6. object[0] = x;
  7. object[1] = y;
  8. object[2]=z;
  9. return object;
  10. }
  11.  
  12. public static void main(String... args) {
  13. Object object[] = wharever();
  14. int y = Integer.parseInt( object[0].toString());
  15. int x = Integer.parseInt( object[1].toString());
  16. boolean z=Boolean.parseBoolean(object[2].toString());
  17. System.out.println(y);
  18. System.out.println(x);
  19. System.out.println(z);
  20. }
  21.  
i wass accesing the posicion that don't exist (boolean z=Boolean.parseBoolean(object[3].toString());)
Feb 16 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

66 posts views Thread by Darren Dale | last post: by
1 post views Thread by Jack Addington | last post: by
5 posts views Thread by Edward Diener | last post: by
16 posts views Thread by Nikolay Petrov | last post: by
43 posts views Thread by Tim Chase | last post: by
8 posts views Thread by aleksandar.ristovski | last post: by
80 posts views Thread by xicloid | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.