0

My goal is to take the digits out of a string and convert it to an int. For example: 12.7 (string) would become 127 (int) 156-06 (string) would become 15606 (int)

This is the code I used:

private static int convertToDigits(String input){
    StringBuilder sb = new StringBuilder(input.length());
    for(int i = 0; i < input.length(); i++){
        char c = input.charAt(i);
        if(c > 47 && c < 58){
            sb.append(c);
        }
    }
    String result = sb.toString().trim();
    Log.d("Result", result);
    return Integer.parseInt(result);
}

When I log the result I am getting 127 as the string value I want, but when I convert that to an int, I get a NumberFormatException:

java.lang.NumberFormatException: Invalid int: ""
        at java.lang.Integer.invalidInt(Integer.java:138)
        at java.lang.Integer.parseInt(Integer.java:358)
        at java.lang.Integer.parseInt(Integer.java:334)
        at com.dcasillasappdev.mytrackfieldteam.utility.Utility.convertToDigits(Utility.java:333)

Am I missing something here? Thanks in advance!

6
  • stackoverflow.com/questions/2709253/… Commented Oct 2, 2015 at 17:56
  • Can you give an example of an input not handled correctly? Commented Oct 2, 2015 at 17:58
  • are you sure that you are logging result ? Accordingly to the exception, result is empty Commented Oct 2, 2015 at 17:59
  • 1
    java.lang.NumberFormatException: Invalid int: "" An empty string is not 0! Commented Oct 2, 2015 at 18:00
  • Ok so I put inputted 12.7 and here's the log: 10-02 14:07:38.100 10383-10383/com.dcasillasappdev.myapp D/Result﹕ 127 Commented Oct 2, 2015 at 18:08

1 Answer 1

2

Am I missing something here?

Yes, the case of empty input. Your method works fine for non-empty strings, you just need to add special treatment for empty input, for example:

if (input.isEmpty()) {
    return 0;  // or whatever way you want to handle this case ...
}

Btw, here's a simpler way to strip all non-digit characters and convert to an int:

return Integer.parseInt(input.replaceAll("\\D+", ""));

But if you really want to stick to your original approach iterating over the characters and using a string builder, then here's a better way to write that:

private static int convertToDigits(String input) {
    if (input.isEmpty()) {
        return 0;
    }
    StringBuilder builder = new StringBuilder(input.length());
    for (char c : input.toCharArray()) {
        if('0' <= c && c <= '9') {
            builder.append(c);
        }
    }
    return Integer.parseInt(builder.toString());
}
Sign up to request clarification or add additional context in comments.

2 Comments

I also tried running your method and I'm still getting "Invalid int: "". I'm confused why I'm getting an empty input when my log is showing a valid int.
i figured out what went wrong. Janos your answer was helpful.

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.