Yes, I disagree with you and dswastik. Sorry :(
If you use a collection in this case

Quote:

Originally Posted by myself

you should cast and know exactly what the order of the elements in your Collection is

I think you do agree with me that, in this case, there is no real need to return 2 things.

01-08-2009, 02:04 PM

raffaele181188

@MuslimCoder

Quote:

How do you enclose the Java code???

Select the text and then click on the # button (the third from the right)

01-08-2009, 02:07 PM

Eranga

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, 02:11 PM

Eranga

Quote:

Originally Posted by raffaele181188

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.

Yes I agreed with you. Actually what I want to pointed in my first post is, collection is the best choice. But have think about few things as you explain. It's not good with those types use in this code, but for the multiple return it's nice. Isn't it?

01-08-2009, 02:14 PM

raffaele181188

Quote:

Originally Posted by Eranga

What you exactly want to do with this code?

@Eranga
I think MuslimCoder just tried to solve itaipee's problem using arrays

01-08-2009, 02:19 PM

raffaele181188

Quote:

Originally Posted by Eranga

It's not good with those types use in this code, but for the multiple return it's nice. Isn't it?

No it isn't nice...:p It's simply PERFECT :D
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, 02:27 PM

Eranga

Quote:

Originally Posted by raffaele181188

No it isn't nice...:p It's simply PERFECT :D
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).

100 mark to you. I agreed with you lol.

01-08-2009, 04:55 PM

Steve11235

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, 07:56 PM

neilcoffey

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:

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".

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).

01-08-2009, 08:02 PM

raffaele181188

Quote:

Originally Posted by steve

You could just return a String, but that would limit you to one error

Yes, this is the thing itaipee talked about:

Quote:

Originally Posted by itaipee

I want them to return 2 things : boolean for if the check passed. and string for telling what the error is

Quote:

Originally Posted by steve

In the caller, check if the ArrayList has any entries (size() == 0). If there are no entries, there are no errors.

This is one right way if he would like to return MULTIPLE errors, but it doesn't seem so in his original post.

Quote:

Originally Posted by Steve

Using the ArrayList approach, you can also create an ErrorMessage class with multiple fields, which is useful for more advanced applications

Yes, another way to do the task and MuslimCoder argued it, yet. But IF he needs multiple errors

@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, 10:45 AM

itaipee

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, 05:02 PM

CJSLMAN

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:

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.