Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store a number longer than 10 digits in Java

I have a problem with my code

nama=txtNama.getText().trim();
int nim =Integer.parseInt(txtNIM.getText());
alamat=txtAlamat.getText().trim();
int telp =Integer.parseInt(txtTelp.getText());

In the code Integer

int nim =Integer.parseInt(txtNIM.getText());
int telp =Integer.parseInt(txtTelp.getText());

I only can input 10 number, if I have input more than 10 number it will be an error

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1111111111111"

Thanks for your attention and help.

like image 223
Candra Herk Avatar asked Oct 24 '25 05:10

Candra Herk


1 Answers

An Integer in java is 32 bits long, it can only hold values from [-2147483648, 2147483647].

Use a long

long nim = Long.parseLong(txtNIM.getText());
long telp = Long.parseLong(txtTelp.getText());

A long is 64 bits, it can hold values from [-9223372036854775808, 9223372036854775807]

If you must go bigger, you can use BigInteger class.

like image 90
James Wierzba Avatar answered Oct 26 '25 19:10

James Wierzba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!