9

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

7 Answers 7

29

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() )
Sign up to request clarification or add additional context in comments.

Comments

16

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.

4 Comments

+1 to Tom G, using Long or other numeric type can cause issues in future.
This is the correct response. What if the phone number is 000-0001. Do you really want to store it as 1? What would you show the user?
+1 tom, agreed ... way better to make it an string than long .. sad i dint get that tought while answering :P,
I tern the int varible to String ?
4

A Signed 32 Bit Integer can only read upto 2^31. You have to use a bigger datatype. long will get you up to 2^63.

Comments

2

The basic thing is that, we don't need a phone number to be part of a arithmetic calculation like addition, subtraction etc. Hence we can take it as a String safely.

Comments

1
2463025552 

is a out of range for int data type, try giving lesser number. Also check if it is in correct number format (like no spaces etc)

1 Comment

It is a given phone number, we can't really change it so it fits into an int.
0

Change your data type to long or bigint. Your string is too long then int thats why it have exception..

Comments

0

Integer.parseInt( s2[1].trim() ); here is your problem. So, change your parsing Integer to Long

1 Comment

I change from Integer to long and works perfect.. A lot of thanks for help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.