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,050 points
1,266 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.

answered May 1, 2022 by narikkadan
• 59,740 points

Related Questions In Other DevOps Questions

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,230 points
258 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,230 points
695 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,770 points
149 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,120 points
337 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,900 points
4,002 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
321 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
• 59,740 points
1,571 views
0 votes
1 answer

How do I update Node.js?

Use Node Version Manager (NVM). This helps you ...READ MORE

answered Apr 29, 2022 in Other DevOps Questions by narikkadan
• 59,740 points
134 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