Skip to content

[BUG] Potential NumberFormatException in myAtoi.java with non-ASCII Unicode digits Description #7205

@lionel-w2

Description

@lionel-w2

Description

There is a bug in the myAtoi implementation where certain Unicode digit characters (e.g., Arabic-Indic digit '١' U+0661) pass the Character.isDigit() check but cause the parsing logic to fail.

Specifically, when the input contains these characters, the current logic identifies them as digits but fails to accumulate them into a valid numeric string, leading to an empty string being passed to Integer.parseInt(), which throws a NumberFormatException.

Steps to reproduce

Pass a string containing non-ASCII digits, such as "١" (Arabic-Indic digit 1), to the myAtoi function.

The code executes the following sequence:

  • Character.isDigit('١') returns true.

  • The logic proceeds to parse but fails to handle the non-ASCII mapping.

  • Integer.parseInt("") is eventually called (or a similar empty-string conversion).

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

Excepted behavior

The function should follow the "parse as much as possible" rule:

  • If non-ASCII digits are not supported, they should be treated as non-digit characters.

  • The function should return 0 or the successfully parsed integer part instead of throwing an unhandled exception.

Screenshots

Image

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions