Results 1 to 20 of 20
Thread: method that return 2 arguments
- 01-08-2009, 11:31 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 99
- Rep Power
- 0
method that return 2 arguments
hi ,
I need to make methods that check errors in input. I want them to return 2 things : boolean for if the check passed. and string for telling what the error is .
How to do it ?
Java Code://what I want String str=null; if (checkErrorMethod(str)==true) System.out.println("this is the error :"+str);
- 01-08-2009, 11:50 AM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
A method can never return more than one value, so considering you scenario you can think of returning a collection, here is a sample code
import java.util.*;
class A{
public static List getStatusAndMessage(String str){
ArrayList <Object>al=new ArrayList<Object>();
if(str==null){
al.add(new Boolean(false));
al.add("String is null");
}
else{
al.add(new Boolean(true));
al.add("Valid string");
}
return al;
}
public static void main(String args[]){
String str=null;
ArrayList al=(ArrayList)getStatusAndMessage(str);
Boolean b=(Boolean)al.get(0);
boolean flag=b.booleanValue();
if(!flag){
System.out.println(al.get(1));
}
}
}
- 01-08-2009, 11:58 AM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
Hello,
I am not sure if that is possible, Allah knows best. I think you should use a seperate class e.g ErrorChecker then Create variables, which you can access.
- 01-08-2009, 11:59 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, List is the best option you have to do this. If you want to return the same data type in multiple times, you can use an array as well.
- 01-08-2009, 12:34 PM #5
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
This time I disagree with you.
If you use a Collection you must:
- to wrap the bool in a Boolean object
- declare it as a List<Object>, then specifying the type of the returned object with a cast, without taking advantage of generic types.
I think you could simply return a String object and either
- throw an exception
- OR returning null
So your code may look like:
Or (which I think is the best):Java Code:try { ... String error = myChecker.checkError(); System.out.println("Error found: " + error); ... } catch (NoErrorFoundException e) { System.out.println("No errors found"); }
The latter approach is used in the standard java.io package, in which you continue to readLine() until the returned String == null.Java Code:// Note that the checkError() method must return // a descriptive String if an error is found and a null // object otherwise. String error = myChecker.checkError(strToTest); if ( error != null ) { System.out.println("Error found: " + error); } else { System.out.println("No error found"); }
I suggest you not to use collections in this case, bacuse it is simply unnecessary. Moreover you should cast and know exactly what the order of the elements in your Collection is.
Luck
- 01-08-2009, 12:42 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Are you disagreed with my comment raffaele?
- 01-08-2009, 12:59 PM #7
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
I am new to java...
Would you think of this a good use of the Array?
<code>
public class Test
{
public static String[] Check(String s)
{
// do some checking...
String a[] = {"true", "Error......"};
return a;
}
public static void main(String args[])
{
String[] err;
String s = "hello";
err = Check(s);
System.out.println( "Err: " + err[0] + " Description: " + err[1]);
}
}
</code>
How do you enclose the Java code??? :)
- 01-08-2009, 01:02 PM #8
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
Yes, I disagree with you and dswastik. Sorry :(
If you use a collection in this case
I think you do agree with me that, in this case, there is no real need to return 2 things.
Originally Posted by myself
- 01-08-2009, 01:04 PM #9
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
@MuslimCoder
Select the text and then click on the # button (the third from the right)How do you enclose the Java code???
- 01-08-2009, 01:07 PM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What you exactly want to do with this code?
Read our FAQ page on the left-side panel of this page. There are lots of things helpful to you.
- 01-08-2009, 01:11 PM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 01-08-2009, 01:14 PM #12
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
@Eranga
Originally Posted by Eranga
I think MuslimCoder just tried to solve itaipee's problem using arrays
- 01-08-2009, 01:19 PM #13
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
No it isn't nice...:p It's simply PERFECT :D
Originally Posted by Eranga
THAT is the way they are supposed to be used. But usually, when using Collections, you put into them the same type of Object. Then you can:
- Retrieving them without a cast using generics
- use an enhanced for loop to iterate through the elements
But to do these things the elements in your Collection ARE TO BE the same type. Often, when you feel you need a Collection filled with different types, you are making a design mistake (like itaipee).
- 01-08-2009, 01:27 PM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 01-08-2009, 03:55 PM #15
Eranga is right in this case, return a collection is the best approach. To keep it simple, use ArrayList. Using arrays ends up being complicated.
You could just return a String, but that would limit you to one error.
In your checker, create an empty ArrayList, new ArrayList<String>. If errors are found, add messages. Return the ArrayList.
In the caller, check if the ArrayList has any entries (size() == 0). If there are no entries, there are no errors.
Using the ArrayList approach, you can also create an ErrorMessage class with multiple fields, which is useful for more advanced applications.
- 01-08-2009, 06:56 PM #16
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
By the way, an occasionally useful technique for "returning" more than one value is for the caller to pass to the method an array to be filled with a return value. In general, it's not terribly "good design", but it can get you round some of the "object handling messiness" in a few limited cases. For example, if you have a method that you want to return, say, a String and an int, then you could declare the method as follows:
Now, the caller does something such as follows:Java Code:public String performOperation(int[] retVal) { ... retval[0] = integer_to_be_returned; return string_to_be_retured; }
Obviously, you have to decide when you're introducing an ugly API design by doing this and when you're using the pattern to "get you out of a hole".Java Code:int[] intRet = new int[1] ... for (int i = 0; i < noOps; i++) { String str = performOperation(intRet); int intVal = intRet[0]; ... }
There are occasional uses in the JDK (I seem to recall there are examples inside the bowels of Swing for dealing with laying out elements of styled text editors, for example).Neil Coffey
Javamex - Java tutorials and performance info
- 01-08-2009, 07:02 PM #17
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
Yes, this is the thing itaipee talked about:
Originally Posted by steve
Originally Posted by itaipee
This is one right way if he would like to return MULTIPLE errors, but it doesn't seem so in his original post.
Originally Posted by steve
Yes, another way to do the task and MuslimCoder argued it, yet. But IF he needs multiple errors
Originally Posted by Steve
@steve
The fact is, he was told to use Collections to return a Boolean and a String, not multiple errors... So we told him he were the wrong way because it makes little sense to put in the same List a Bool and a String.
@itaipee
If your string may contains multiple errors then you are FORCED to return an array or a Collection. Then your original post is misleading
- 01-11-2009, 09:45 AM #18
Member
- Join Date
- Dec 2008
- Posts
- 99
- Rep Power
- 0
returning an array, list or collection - how should I say it, it does not look nice .
But I guess it is the simplest solution ( I didn't think on it but multiply error may be needed so returning one String may also not do the trick)
@Rafael
the all "try" and "catch" look like best , but I have to learn how to use it
- 01-12-2009, 04:02 PM #19
Return string?
If you don't like returning the array, return a string with the all the info you need and just parse it.
something like:
msg = "false: error msg"
or
msg = "true"
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-12-2009, 05:36 PM #20
It's not that hard
Of course, the Message class could contain other information as well, or just text. If the ArrayList has no messages, no error occurred. This approach is simple and powerful, and it doesn't involve any kludge code for parsing.Java Code:public class Message { private int _ID; private final String _Text; public Message(final int pID, final String pText) { _ID = pID; _Text = pText; } public int getID() { return _ID; } public String getText() { return _Text; } } . . . public ArrayList<Message> validate() { final ArrayList<Message> messages = new ArrayList<Message>(); if (some error) { messages.add(1, "Some error occurred."); } return messages; } . . . final ArrayList<Message> messages = validate(); if (messages.size() > 0) { // display the messages // skip processing } // do processing
Similar Threads
-
Static Method and Return Statements
By berelson in forum New To JavaReplies: 2Last Post: 11-29-2008, 11:17 PM -
return a null method
By valoyivd in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:19 PM -
Using final with method arguments
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:48 PM -
Return question in a method.
By MetalGear in forum New To JavaReplies: 1Last Post: 01-13-2008, 04:45 AM -
Return value of method
By cachi in forum New To JavaReplies: 1Last Post: 08-01-2007, 08:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks