473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 32145
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

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

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
15
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
1
by: Jack Addington | last post by:
I have a 3rd party object that fires an itemchanged event when someone edits some data on a form. This event has a custom eventArgs that has a field called ActionCode. In the code of the event,...
5
by: Edward Diener | last post by:
I am gathering from the documentation that return values from __events are not illegal but are frowned upon in .NET. If this is the case, does one pass back values from an event handler via...
16
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
43
by: Tim Chase | last post by:
Just as a pedantic exercise to try and understand Python a bit better, I decided to try to make a generator or class that would allow me to unpack an arbitrary number of calculatible values. In...
8
by: aleksandar.ristovski | last post by:
Hello all, I have been thinking about a possible extension to C/C++ syntax. The current syntax allows declaring a function that returns a value: int foo(); however, if I were to return...
80
by: xicloid | last post by:
I'm making a function that checks the input integer and returns the value if it is a prime number. If the integer is not a prime number, then the function should return nothing. Problem is, I...
2
ADezii
by: ADezii | last post by:
The incentive for this Tip was an Article by the amazing Allen Browne - I considered it noteworthy enough to post as The Tip of the Week in this Access Forum. Original Article by Allen Browne ...
4
by: Jonathan | last post by:
I have a SQL stored procedure for adding a new record in a transactions table. It also has two return values: CounterID and IDKey. I want to create a webservice that accepts the 10 input...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.