How to group all the items into array and check them?

C

cybernerdsx2

Hi,

I would like to list all the error codes into a fixed array and then
use it to check if the item falls into the array.

Something like psudo code below:

if ( errorCode isIn errorsArray[] ) then
Display "Error found matching inside the array"
Else
Display "No macthing error found"

Is there such a keyword found in Java? Or is there such a method to do
it?
 
A

Andrey Kuznetsov

I would like to list all the error codes into a fixed array and then
use it to check if the item falls into the array.

Something like psudo code below:

if ( errorCode isIn errorsArray[] ) then
Display "Error found matching inside the array"
Else
Display "No macthing error found"

Is there such a keyword found in Java? Or is there such a method to do
it?

if your error code just numbers from 0 till xxx, then you can just
if(errorCode < errorsArray.length) { ....//errorCode is an int

if not you can put them into Hashtable and do following
if(errosTable.get(errorCode) != null) {... //errorCode is Object

in worst case you can search your array:
for(int i = 0; i < errorsArray.length;i++) {
if(errorsArray == errorCode) {... //errorCode is an int
}
 
W

Wendy S

Andrey Kuznetsov said:
if not you can put them into Hashtable and do following
if(errosTable.get(errorCode) != null) {... //errorCode is Object

I was also going to suggest a Map.

If you just need to know whether it's in there (and not retrieve it):
if( errorsTable.containsKey( errorCode) ) { ... }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top