Questions tagged [numberformatexception]

A NumberFormatException occurs in Java code when a programmer tries to convert a String into a number and the conversion fails.

A NumberFormatException is thrown to indicate that an attempt to parse numeric information in a string has failed. NumberFormatException is a subclass of the RuntimeException class. The number might be int, float, or any of Java's numeric values.

833 questions
110
votes
10 answers

Convert hex color value ( #ffffff ) to integer value

I am receiving hex color values from a server (in this form, #xxxxxx , example #000000 for black) How do I convert this to an integer value? I tried doing Integer.valueOf("0x" + passedColor.substring(1, passedColor.length())) to get an even more…
CQM
  • 42,592
  • 75
  • 224
  • 366
99
votes
6 answers

How can I prevent java.lang.NumberFormatException: For input string: "N/A"?

While running my code 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…
codemaniac143
  • 1,241
  • 2
  • 11
  • 18
96
votes
2 answers

Why do Double.parseDouble(null) and Integer.parseInt(null) throw different exceptions?

Why do Double.parseDouble(null) and Integer.parseInt(null) throw different exceptions? Is this a historical accident or intentional? The documentation clearly states two types of exceptions for Double.parseDouble(...) and one for Integer.parseInt(),…
pho79
  • 1,755
  • 1
  • 15
  • 27
39
votes
10 answers

Double value to round up in Java

I have a double value = 1.068879335 i want to round it up with only two decimal values like 1.07. I tried like this DecimalFormat df=new DecimalFormat("0.00"); String formate = df.format(value); double finalValue = Double.parseDouble(formate)…
jimmy
  • 8,121
  • 11
  • 36
  • 40
32
votes
9 answers

What is a NumberFormatException and how can I fix it?

Error Message: Exception in thread "main" java.lang.NumberFormatException: For input string: "Ace of Clubs" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) …
Qasim Imtiaz
  • 519
  • 2
  • 8
  • 12
28
votes
6 answers

Why NumberFormatException is runtime?

Runtime exceptions indicate broken contract (like NPE) and should never be thrown if code has no errors. It always indicates error in code (same as asserts but asserts are for internal class errors while Runtime are for class's client…
Ilya.K
  • 567
  • 6
  • 7
21
votes
8 answers

What is the proper way to handle a NumberFormatException when it is expected?

I'm running into this situation where I need to parse a String into an int and I don't know what to do with the NumberFormatException. The compiler doesn't complain when I don't catch it, but I just want to make sure that I'm handling this…
Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
16
votes
5 answers

How to programmatically replace Spring's NumberFormatException with a user-friendly text?

I am working on a Spring web app and i have an entity that has an Integer property which the user can fill in when creating a new entity using a JSP form. The controller method called by this form is below : @RequestMapping(value =…
Martin
  • 1,977
  • 5
  • 30
  • 67
16
votes
5 answers

Cannot convert String to Integer in Java

I have written a function to convert string to integer if ( data != null ) { int theValue = Integer.parseInt( data.trim(), 16 ); return theValue; } else return null; I have a string which is 6042076399 and it gave…
Ding
  • 163
  • 1
  • 1
  • 4
15
votes
13 answers

How to avoid Number Format Exception in java?

In my day to day web application development there are many instances where we need to take some number inputs from the user. Then pass on this number input to may be service or DAO layer of the application. At some stage since its a number…
ajm
  • 12,863
  • 58
  • 163
  • 234
14
votes
4 answers

Converting 32-bit binary string with Integer.parseInt fails

Why does this part of code fail: Integer.parseInt("11000000000000000000000000000000",2); Exception in thread "main" java.lang.NumberFormatException: For input string: "11000000000000000000000000000000" As far as I understand Integer is a 32 bit…
Aliens
  • 984
  • 3
  • 14
  • 23
12
votes
3 answers

parseDouble in Java results to NumberFormatException

I am trying to load info from a properties file and i have the following code: anInt = Integer.parseInt(prop.getProperty("anInt")); aDouble = Double.parseDouble(prop.getProperty("aDouble")); and while the first line works just fine, the second one…
nikos
  • 2,893
  • 11
  • 30
  • 39
12
votes
3 answers

Exception to Number Format Exception with "D" and "F"?

I have run into a very strange problem in my code. I have a simple temperature converter where the user enters the temperature in Celsius and, after pressing "Convert", the temperature in Fahrenheit is shown. If the user does not enter something…
ninge
  • 1,592
  • 1
  • 20
  • 40
11
votes
5 answers

Nullsafe Long valueOf

Imagine the following scenario Long critId = Long.valueOf(criteriaIdentifier); If the view is initially loaded criteriaIdentifier may be null, afterwards I set the value depending of the selectedItem in the JSF View. However,…
0x45
  • 779
  • 3
  • 7
  • 26
11
votes
5 answers

java.lang.NumberFormatException: For input string: "1,167.40"

I'm reading data from CSV file. One of the fields contains the value 1,167.40. The code piece for reading this field is the following: String csvFilename = "TEST_FILE.csv"; CSVReader csvReader = new CSVReader(new FileReader(csvFilename)); String[]…
Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217
1
2 3
55 56