How can I prevent java lang NumberFormatException For input string N A

0 votes

 I am getting a NumberFormatException:

java.lang.NumberFormatException: For input string: "N/A"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.valueOf(Unknown Source)
    at java.util.TreeMap.compare(Unknown Source)
    at java.util.TreeMap.put(Unknown Source)
    at java.util.TreeSet.add(Unknown Source)`

How can I stop this exception from happening?

Apr 30, 2022 in Other DevOps Questions by Kichu
• 19,040 points
3,299 views

1 answer to this question.

0 votes

"N/A" is not an integer so check before parsing or handling Exceptions properly.

Exception Handling.

try{
    int i = Integer.parseInt(input);
} catch(NumberFormatException ex){ // handle your exception
    ...
}

Integer pattern matching.

String input=...;
String pattern ="-?\\d+";
if(input.matches("-?\\d+")){ // any positive or negetive integer or not!
 ...
}

These are the two options you can try. It has worked for me and hope it works for you as well.

Check out Java course to learn more about it.

Thanks!

answered May 1, 2022 by narikkadan
• 63,600 points

Related Questions In Other DevOps Questions

0 votes
0 answers
0 votes
0 answers
0 votes
1 answer

How do I enable Slack for Bluemix DevOps services beta

Here is where you can take help ...READ MORE

answered Jul 11, 2018 in Other DevOps Questions by Atul
• 10,240 points
577 views
0 votes
1 answer

How do I set variable if a specific package version is installed in CFEngine?

Here is what you can do.Just use packagesmatching to ...READ MORE

answered Jul 12, 2018 in Other DevOps Questions by Atul
• 10,240 points
1,180 views
0 votes
0 answers

Java - Convert integer to string

Given a number: int number = 1234; Which would ...READ MORE

Sep 22, 2022 in Java-Script by Tejashwini
• 3,820 points
570 views
0 votes
1 answer

How can two strings be concatenated in java?

You can concatenate Strings using the + operator: System.out.println("Your number ...READ MORE

answered Jun 6, 2018 in Java by Daisy
• 8,140 points
823 views
0 votes
2 answers

How an object array can be converted to string array in java?

System.arraycopy is the most efficient way, but ...READ MORE

answered Aug 8, 2018 in Java by Sushmita
• 6,920 points
5,656 views
0 votes
1 answer

Why is char[] preferred over a string?

Strings are immutable. That means once you've ...READ MORE

answered Jun 14, 2018 in Java by scarlett
• 1,290 points
666 views
0 votes
1 answer

Java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse

Just follow these two steps: Step 1 Add This ...READ MORE

answered Apr 29, 2022 in Other DevOps Questions by narikkadan
• 63,600 points
2,707 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP