473,581 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to return 2 values

4 New Member
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.getSe lectedIndex();
switch (tabIndex)
{
case BulletinConstan ts.INPUT_TAB_IN DEX:
_bulletinItemTa b.init(null);
x = true;
y = false;
Log.enter(getCl ass(),"Tab moved to Input");
break;
case BulletinConstan ts.RELAY_TAB_IN DEX:
_relayTabAdapte r.setLogger(get Logger());
_bulletinRelayT ab.init();
Log.enter(getCl ass(),"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 32159
r035198x
13,262 MVP
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.getSe lectedIndex();
switch (tabIndex)
{
case BulletinConstan ts.INPUT_TAB_IN DEX:
_bulletinItemTa b.init(null);
x = true;
y = false;
Log.enter(getCl ass(),"Tab moved to Input");
break;
case BulletinConstan ts.RELAY_TAB_IN DEX:
_relayTabAdapte r.setLogger(get Logger());
_bulletinRelayT ab.init();
Log.enter(getCl ass(),"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 Recognized Expert MVP
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
java2986
2 New Member
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 Recognized Expert MVP
@java2986
Please compile and run/test your code before you post it; there's an ArrayIndexOutOf BoundsException in your code.

kind regards,

Jos
Feb 16 '09 #5
java2986
2 New Member
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.parse Boolean(object[3].toString());)
Feb 16 '09 #6

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

Similar topics

66
4948
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
2792
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
1
4657
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, if you set this ActionCode to various values the control will respond in various ways (reject, reject change field, accept, etc). I am trying to...
5
2062
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 "in/out" or "out" parameters ? Or is it simply that events are just notifications and are not interested in any values which event handlers might be able...
16
17387
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
43
3350
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 this case, just zeros (though I just to prove whatever ends up working, having a counting generator would be nice). The target syntax would be...
8
5196
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 more than one value, for example three int-s, I would have to change my "logic" and pass the references to my
80
41076
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 don't know how to do that. Isn't there anything like "return null"in C?
2
3663
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 Traditionally, one has always thought that Functions can only return a single value, and for the most part that was true. Ever since Access 95, we...
4
7132
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 parameters and returns the two return values. My C# programmer here says that webservice methods can only return 1 value per method. Is that right?...
0
7804
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7910
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8180
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1409
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1144
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.