NumberFormatException occured

hi all

strPan = txtPan.getText();

Long iPAN = 0;

try {

iPAN = Long.parseLong(strPan.trim());

} catch(NumberFormatException e) {

String strErrMsg = "The PAN must be numeric";

JOptionPane.showMessageDialog(this, strErrMsg,"Error", JOptionPane.ERROR_MESSAGE);

return;

}

if(iPAN == 0 || iPAN >24) {

String strErrMsg = "The PAN must lie in the range 1 to 24";

JOptionPane.showMessageDialog(this, strErrMsg, "Error", JOptionPane.ERROR_MESSAGE);

return;

}

when i enter the PAN as 121212121212121212121212121212.

i got "The PAN must be numeric"

can any one help me what is the error.?

[690 byte] By [Inspiron123a] at [2007-10-3 2:33:31]
# 1
Well for starts you cannot convernt from int to long..... Long iPAN = 0;
Tom_Timpsona at 2007-7-14 19:32:31 > top of Java-index,Java Essentials,New To Java...
# 2

Also 121212121212121212121212121212 isnt a long......

if passing in a correct long try the following

strPan = txtPan.getText();

long iPAN = 0; // updated your Long to my long

try {

> iPAN = Long.valueOf(strPan).longValue(); // converted differently....

} catch(NumberFormatException e) {

Tom_Timpsona at 2007-7-14 19:32:31 > top of Java-index,Java Essentials,New To Java...
# 3

> hi all

>

> strPan = txtPan.getText();

> Long iPAN = 0;

> try {

> iPAN = Long.parseLong(strPan.trim());

> } catch(NumberFormatException e) {

> String strErrMsg = "The PAN must be

> numeric";

> JOptionPane.showMessageDialog(this, strErrMsg,

> ,

The following does not look like it will compile unless maybe autoboxing is allowing it:

iPAN = Long.parseLong(strPan.trim());

You should display strPan too be sure that it contains the string you expect.

Also, show us the actual stack trace with e.StackTrace().

jbisha at 2007-7-14 19:32:31 > top of Java-index,Java Essentials,New To Java...
# 4
hi Tom_Timpsoneven this throws NumberFormatExceptionits there any other way. to convert string to long
Inspiron123a at 2007-7-14 19:32:31 > top of Java-index,Java Essentials,New To Java...
# 5
hi jbisheven this is throws an exception for the strPan="121212121212121212121212121212"iPAN = Long.parseLong(strPan.trim());
Inspiron123a at 2007-7-14 19:32:31 > top of Java-index,Java Essentials,New To Java...
# 6

> even this throws NumberFormatException

What String are you passing in? remember if you want to convert from a string to a long, the input MUST be a long.......

> its there any other way. to convert string to long

Yes long l = Long.valueOf(str).long.Value();

This will convert the input string "str" into it's long equivalence.

Is this any help?

Tom_Timpsona at 2007-7-14 19:32:31 > top of Java-index,Java Essentials,New To Java...
# 7

> its there any other way. to convert string to long

Also :

// String s = "fred";// do this if you want an exception

String s = "100";

try {

long l = Long.parseLong(s.trim());

System.out.println("long l = " + l);

} catch (NumberFormatException nfe) {

System.out.println("NumberFormatException: " + nfe.getMessage());

}

OR

long l = Long.parseLong("123");

Tom_Timpsona at 2007-7-14 19:32:31 > top of Java-index,Java Essentials,New To Java...
# 8

what am saying is

// String s = "fred";// do this if you want an exception

String s = "12121212121212121212";

try {

long l = Long.parseLong(s.trim());

System.out.println("long l = " + l);

} catch (NumberFormatException nfe) {

System.out.println("NumberFormatException: " + nfe.getMessage());

}

Inspiron123a at 2007-7-14 19:32:31 > top of Java-index,Java Essentials,New To Java...
# 9
I believe the reason you are getting an exception is that your number is too big to fit into a long.From the JLS:For long, from -9223372036854775808 to 9223372036854775807, inclusive1212121212121212121212121212129223372036854775807
jbisha at 2007-7-14 19:32:31 > top of Java-index,Java Essentials,New To Java...
# 10
The reason your getting number format exception is simpily becasue 12121212121212121212 is not a long.is there any other type you can use for iPan?
Tom_Timpsona at 2007-7-14 19:32:31 > top of Java-index,Java Essentials,New To Java...
# 11
> 121212121212121212121212121212> 9223372036854775807Exactly..... there is no possible way of trying to do what you want...... unless you want to break the long down and add it all up again, but that might be a bit messy.
Tom_Timpsona at 2007-7-14 19:32:31 > top of Java-index,Java Essentials,New To Java...
# 12
If you really need to check numbers this large, either check each character in the String to see if it is a number or use the BigInteger class: http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigInteger.html
jbisha at 2007-7-14 19:32:31 > top of Java-index,Java Essentials,New To Java...
# 13

> i enter the PAN as 121212121212121212121212121212.

> i got "The PAN must be numeric"

> can any one help me what is the error.?

PAN as in Primary Account Number (as with credit cards)?

Then stop trying to convert it. It should be manipulated as a string and not a numeric.

jschella at 2007-7-14 19:32:31 > top of Java-index,Java Essentials,New To Java...