Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NumberFormatException: unable to parse String as integer

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

like image 759
Aftab Ali Avatar asked Dec 08 '22 20:12

Aftab Ali


2 Answers

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

like image 161
Dmitri Avatar answered May 20 '23 10:05

Dmitri


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.

like image 31
Bhesh Gurung Avatar answered May 20 '23 09:05

Bhesh Gurung