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

4
  • Phone numbers are not number. You should store them as strings. Commented Nov 1, 2012 at 2: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. Commented Nov 1, 2012 at 2: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. Commented Nov 1, 2012 at 2:43
  • try cell=Integer.ParseInteger("03462651882"); instead of cell=Integer.valueOf("03462651882"); this one... Commented Nov 1, 2012 at 7:33

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

@ 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.
Integer.MAX_VALUE == 2147483647 - it's a regular signed 32-bit int.
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.

Comments

1

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

Comments

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();    
    }     

Comments

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.