-1

In Java I try to convert a string value into an integer, removing the 4 last characters, so I tried like that :

 String filename1="98597598684.txt";
 int id = Integer.parseInt(filename1.substring(0,  filename1.length()-4));

but I get this error, and I don't understand why :

 java.lang.NumberFormatException: For input string: "98597598684"
at java.lang.NumberFormatException.forInputString(Unknown Source)

it's probably sthg simple but that makes me crazy since 1 hour, any idea?

Julien
  • 3,743
  • 9
  • 38
  • 67
  • 2
    Duplicates: http://stackoverflow.com/questions/27331336/java-lang-numberformatexception-invalid-int-3546504756-what-does-this-error, http://stackoverflow.com/questions/31846838/integer-parsestring-str-java-lang-numberformatexception-errors – rgettman Oct 27 '15 at 16:17
  • 1
    A solution-seeking tip: in this case you would have probably found answers in far less than an hour if you had googled **"at java.lang.NumberFormatException.forInputString"**. That produced this existing stackoverflow thread for me as the first hit: http://stackoverflow.com/questions/13935167/java-lang-numberformatexception-for-input-string . Exceptions are your friend, google the living daylight out of them :) – Gimby Oct 27 '15 at 16:19

2 Answers2

6

98597598684 is greater than Integer.MAX_VALUE. Use

long id = Long.parseLong(filename1.substring(0, filename1.length() - 4));
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

you can even try new BigInteger("98597598684")

AbtPst
  • 7,778
  • 17
  • 91
  • 172