Uploaded image for project: 'Commons Math'
  1. Commons Math
  2. MATH-198

java.lang.StringIndexOutOfBoundsException in ComplexFormat.parse(String source, ParsePosition pos)

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.2
    • None
    • None
    • None
    • Ubuntu 7.10, JDK 6.0, PC

    Description

      The parse(String source, ParsePosition pos) method in the ComplexFormat class does not check whether the imaginary character is set or not which produces StringIndexOutOfBoundsException in the substring method :

      (line 375 of ComplexFormat)
      ...
      // parse imaginary character
      int n = getImaginaryCharacter().length();

      startIndex = pos.getIndex();
      int endIndex = startIndex + n;
      if (source.substring(startIndex, endIndex).compareTo(
      getImaginaryCharacter()) != 0) {
      ...
      I encoutered this exception typing in a JTextFied with ComplexFormat set to look up an AbstractFormatter.
      If only the user types the imaginary part of the complex number first, he gets this exception.

      Solution: Before setting to n length of the imaginary character, check if the source contains it. My proposal:
      ...
      int n = 0;
      if (source.contains(getImaginaryCharacter()))
      n = getImaginaryCharacter().length();
      ...

      F.S.

      Attachments

        Activity

          People

            Unassigned Unassigned
            fsala Frederick Salardi
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: