Check if value is an int...

I am sending and receiving data from SNMP traps.

When I get the value back when requesting a read, it gives me a VariableObject, I convert that to a string as VariableObject has that method.

Some of the values being returned are numbers, and I am comparing them between a known set of numbers.

The issue is, if a Null is returned, then it throws a number format exception.

How can I check to see if the returned item is a int and not a string so I know what to do with it?

        PDU responsePDU = response.getResponse();
        for ( int i = 0; i < responsePDU.size(); i++ ) {
            VariableBinding vb = responsePDU.get( i );
            if (min == 0 && max == 0 && dataType.equals("string")) {
                if (vb.getVariable().toString().equals(defaultValue)) {
                    apclog.info("Read: " + vb.getOid().toString() + " = " + vb.getVariable().toString() + " [PASS]");
                    result = true;
                } else {
                    apclog.error("Read: " + vb.getOid().toString() + " = " + vb.getVariable().toString() + " [FAIL]");
                    result = false;
                }
            } else if (min < 0 && max > 0 && dataType.equals("integer")) {
                String recValueString = vb.getVariable().toString();
                    recValueInt = Integer.parseInt(vb.getVariable().toString());
                if (recValueString.equals("Null")) {
                    apclog.error("Read: " + vb.getOid().toString() + " = " + recValueString + " [FAIL]");
                    result = false;
                } else {
                    recValueInt = Integer.parseInt(recValueString);
                    if (recValueInt > min && recValueInt < max && recValueString != null) {
                        apclog.info("Read: " + vb.getOid().toString() + " = " + recValueString + " [PASS]");
                        result = true;
                    } else {
                        apclog.error("Read: " + vb.getOid().toString() + " = " + recValueString + " [FAIL]");
                        result = false;
                    }
                }
               
            }
        }
LVL 2
k41d3nAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSr. Software engineerCommented:
>> if a Null is returned, then it throws a number format exception.
Can't you check for that before trying to parse?
zzynxSr. Software engineerCommented:
Object o = vb.getVariable();
if (o==null) {
} else {
   // your current stuff
}

...or don't I understand you well?
dberner9Commented:
you can catch the NumberFormatException and deal with it accordingly:

try {
     recValueInt = Integer.parseInt(vb.getVariable().toString());
} catch (NumberFormatException ex) {
     recValueInt = -1
}

you can also use the following to determine if the string is numeric:

if (vb.getVariable().toString().matches("\\d+")) {
     recValueInt = Integer.parseInt(vb.getVariable().toString());
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

StillUnAwareCommented:
what is dataType? how do You set it to "string" or "integer"?
dberner9Commented:
bleh... add a ; to the catch block in my code... I really should proofread this :)
k41d3nAuthor Commented:
zzynx,

I couldn't check for null because the variable binding doesn't return what jave reads as null.

It's more like a vector

OID, variable

so if you just print out the variable binding response it looks like:

.1.2.3.4.5.6 = Null

you can get the values in the variable binding by getOID and getVariable

then you convert those to strings and work with them from there. If I check for null, it never finds it.

dberner9,

Your try/catch did do what I needed, though I had to put the number to -1000 to be safe. The variable returned can be from -40 to 100.

Essentially, if you're interested, or for anyone who might need the help later, I am sending requests to SNMP traps and getting the response, or setting values through the request and reading to make sure the value was accepted.

I took a data dictionary of SNMP data points and got the essential data I needed. OID code, MIB address, data type, read only or read/write, default value, and range.

I turned this csv into an XML sheet.

Then I created an SnmpNode object that reads each row in the xml and gets the values out. I add the object to a list so I have all the values from the XML.

Then if the access is Read Only and the data type is an integer, I pass the OID and the range to a method that requests the OID from the SNMP trap, and gets the value returned. I turn the value into an int and check to see if it's between the range for that object.

If it is Read Only and a string, I request the OID and compare the variable agains the default. I need to come up with something better than this, perhaps just a regex that checks to see that it is a string, and is in the character limit range.

if it is Read/Write I write the default, read it back, then write a new value and read that back for measure.

Long winded, and you probably don't care :)

but yes, this worked:

        PDU responsePDU = response.getResponse();
        for ( int i = 0; i < responsePDU.size(); i++ ) {
            VariableBinding vb = responsePDU.get( i );
            try {
                recValueInt = Integer.parseInt(vb.getVariable().toString());
            } catch (NumberFormatException ex) {
                recValueInt = -1000;
            }
            if (dataType.equals("string")) {
                if (vb.getVariable().toString().equals(defaultValue)) {
                    apclog.info("Read: " + vb.getOid().toString() + " = " + vb.getVariable().toString() + " [PASS]");
                    result = true;
                } else {
                    apclog.error("Read: " + vb.getOid().toString() + " = " + vb.getVariable().toString() + " [FAIL]");
                    result = false;
                }
            } else if (dataType.equals("integer")) {
                if (recValueInt != -1000) {
                    if (recValueInt > min && recValueInt < max) {
                        apclog.info("Read: " + vb.getOid().toString() + " = " + vb.getVariable().toString() + " [PASS]");
                        result = true;
                    } else {
                        apclog.error("Read: " + vb.getOid().toString() + " = " + vb.getVariable().toString() + " [FAIL]");
                        result = false;
                    }
                } else {
                    apclog.error("Read: " + vb.getOid().toString() + " = " + vb.getVariable().toString() + " [FAIL]");
                    result = false;
                }
            }
        }
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.