4

I was currently working on my android project when I came across this issue

I want this to convert a String like "0345303709"7 into an integer, but I keep getting a NumberFormatException.

I've searched all the questions here, but have not found a solution to my problem.

Below is my Android code:

 String edit_cell=cellnumber.getText().toString();
        try
        {
            if(cellnumber.getText().length()==11 && TextUtils.isEmpty(edit_cell)!=true && edit_cell!=null)
            {

                cell=Integer.valueOf("03462651882");
            }
            else
            {                   
                Toast.makeText(this, "Invalid CellNumber\n Write CellNumber like this Format:\nNetworkCode Followed by your Number\n",Toast.LENGTH_LONG).show();
                Toast.makeText(this, "eg:03213213214",Toast.LENGTH_LONG).show();    
            }
        }

        catch(Exception ex)
        {
            Toast.makeText(this, "Invalid cellnumber\n Write cellNumber line this format:\n Network code followed by your number\n",Toast.LENGTH_LONG).show();
            Toast.makeText(this, "eg:03213213214",Toast.LENGTH_LONG).show();    
        }      

I am using eclipse Helios IDE and android version is 2.2 api 8

Shellum
  • 3,159
  • 2
  • 21
  • 26
Aftab Ali
  • 201
  • 1
  • 6
  • 16
  • **Phone numbers are not number**. You should store them as strings. – SLaks Nov 01 '12 at 02:31
  • @Slaks i am validating the number for login If a user just entered a decimal number not a cellNumber then application would crash so I am doing this to validate. – Aftab Ali Nov 01 '12 at 02:36
  • 1
    Still a bad idea, you should use a regex to validate. It's also really annoying when applications force a format for phone numbers - just let users enter it however they want, strip out all non-digit characters and validate the length/prefix/whatever else. – Dmitri Nov 01 '12 at 02:43
  • try cell=Integer.ParseInteger("03462651882"); instead of cell=Integer.valueOf("03462651882"); this one... – Aamirkhan Nov 01 '12 at 07:33

4 Answers4

7

It's too big for an Integer, you need a Long.

Edit

Didn't notice that it was a phone number - definitely store it as a String.

As for validation, a lot of people have dealt with that problem before. See here, for example: A comprehensive regex for phone number validation

Community
  • 1
  • 1
Dmitri
  • 8,999
  • 5
  • 36
  • 43
  • @ Dmitri can you tell me the max limit of integer in Value I come from c# their we have int32,int64 so i never had this problem over their. – Aftab Ali Nov 01 '12 at 02:29
  • `Integer.MAX_VALUE == 2147483647` - it's a regular signed 32-bit int. – Dmitri Nov 01 '12 at 02:31
5

That's because the value is too much for an integer to handle. The maximum value an integer can handle is 2147483647, here.

You can use long (Long.valueOf()) instead.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
1

maximum value an integer is 2^31-1 < 3 462 651 882, so you must use long type.

Trần Sĩ Long
  • 457
  • 3
  • 15
1

Because of higher value for Int you are getting

java.lang.NumberFormatException: For input string: "03453037097"

Just replace the int value with long as like this

String edit_cell=cellnumber.getText().toString();
    try
    {
        if(cellnumber.getText().length()==11 && TextUtils.isEmpty(edit_cell)!=true && edit_cell!=null)
        {

            cell=Long.parseLong("03462651882");//=====> Change is here
        }
        else
        {                   
            Toast.makeText(this, "Invalid CellNumber\n Write CellNumber like this Format:\nNetworkCode Followed by your Number\n",Toast.LENGTH_LONG).show();
            Toast.makeText(this, "eg:03213213214",Toast.LENGTH_LONG).show();    
        }
    }

    catch(Exception ex)
    {
        Toast.makeText(this, "Invalid cellnumber\n Write cellNumber line this format:\n Network code followed by your number\n",Toast.LENGTH_LONG).show();
        Toast.makeText(this, "eg:03213213214",Toast.LENGTH_LONG).show();    
    }     
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78