Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NumberFormatException: For input string

Tags:

java

the eclipse tells that lang and i cant find a solution

Exception in thread "main" java.lang.NumberFormatException: For input string: "2463025552" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at Main.main(Main.java:31)

String s2[]=s.split("\\,");
Records rec=new Records();
rec.setName(s1[0]);
rec.setAddres(s2[0]);

phone  = Integer.parseInt( s2[1].trim() );
System.out.println(phone);

I read from file in this format name-adres,phone and ad in arraylist put for phone i have problem

like image 363
Βίκτορας ανδρεάδης Avatar asked Dec 18 '12 14:12

Βίκτορας ανδρεάδης


2 Answers

Integer.parseInt throws a NumberFormatException if the passed string is not a valid representation of an integer. here you are trying to pass 2463025552 which is out of integer range.

use long instead

long phone = Long.parseLong(s2[1].trim() )
like image 134
PermGenError Avatar answered Sep 17 '22 17:09

PermGenError


The real problem is that a phone number is not an integer. It is a String. You should not store it as a number, for reasons similar to the problem you are encountering now. The same applies for ZIP codes, sports team's jersey numbers, and a host of other "fake" numbers.

like image 41
Thorn G Avatar answered Sep 20 '22 17:09

Thorn G