1

I'm trying to convert a string that contains just numbers to integer using int number = Integer.parseInt(string), but it returns #

error: Invalid int: "number"

For example, if the string is "10", it returns: Invalid int: "10". What is wrong?

Edit:

FileInputStream fis;
        int number =0;
        String line="";
        try
        {
            fis = new FileInputStream(getFilesDir()+pathToFile);
            BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
            line=reader.readLine();
            Log.e("Read Line", "Read line: *" +line+ "*"); //returns *10*
            try
            {
                number = Integer.parseInt(line.trim());
            }
            catch(NumberFormatException nfe)
            {
                Log.e("ERROR CONVERTING", nfe.getMessage()); // return above error
            }
            reader.close();

        }
        catch(Exception ex)
        {
            Log.e("ERROR OPENING FILE", "Can't open file: "+ex.getMessage());
        }
9
  • might be white space around string. try Integer.parseInt(string.trim()); Commented Feb 6, 2013 at 14:30
  • Are you sure it's "ten" but not "one o"? I mean, check if zero is a zero, not caps o Commented Feb 6, 2013 at 14:30
  • 1
    I suspect the string isn't just "10". Try logging its length as well - there may be some non-printable characters. Commented Feb 6, 2013 at 14:31
  • Also, "l" (lower case L) looks similar to "1" (one) Commented Feb 6, 2013 at 14:31
  • 3
    If you post the whole exact error we can help way better. Commented Feb 6, 2013 at 14:32

3 Answers 3

4

I used the code in the answer from this question: LINK

str = str.replaceAll("\\D+","");

Don't know why it doesn't work without this, because there are no other characters besides numbers in the string, but not it works.

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

Comments

2

Some times it means "the number is too big".

I come across a situation where it happens because my number was too big. Example:

Integer.parseInt("201506121234567");

It causes me the exception NumberFormat.

To solve, I changed to this:

Long.parseLong("201506121234567");

Hope it helps someone!

Comments

1

Did you actually try with "10" or any other number? The error is pretty specific.

Invalid int: "number"

This means that in this line

Integer.parseInt(string)

string has the value "number". Check where are you setting this value and verify that you're actually setting a numeric string.

3 Comments

From OP: if the string is "10", it returns: Invalid int: "10"
@m0skit0 Actually, the OP started that sentence with For example. I'm not too sure he actually tried that.
Then I'd use a regex to actually depure the string and find the numeric characters on it.

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.