jscience
  1. jscience
  2. JSCIENCE-54

Amount.valueOf(CharSequence) fails for values without unit

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: current
    • Fix Version/s: Version 6.0
    • Component/s: www
    • Labels:
      None
    • Environment:

      Operating System: All
      Platform: All

    • Issuezilla Id:
      54

      Description

      Some cases where Amount.valueOf() fails are Amount.valueOf("1"),
      Amount.valueOf("1.0"), and Amount.valueOf("1.0 "). However, Amount.valueOf("1
      ") succeeds.

      I would expect all the cases to create a Dimensionless Amount result (Unit.ONE
      units).

      I didn't try using the other AmountFormat instances to see if they had a similar
      result.

      The following source and output demonstrates the cases shown above. I was using
      JScience 4.1.2 for this example.

      ------------- Start of Java source ---------------
      import org.jscience.physics.amount.Amount;

      public class AmountValueOf {
      public static void main(String[] args)

      { test( "1" ); // fails test( "1 " ); // works test( "1.0" ); // fails test( "1.0 " ); // fails }

      private static void test(String valueStr) {
      System.out.println();
      System.out.println( "**** Begin value string \"" + valueStr + "\"" );
      try

      { Amount<?> amount = Amount.valueOf(valueStr); System.out.println( "amount = \"" + amount + "\"" ); }

      catch (RuntimeException ex)

      { ex.printStackTrace(System.out); }

      finally

      { System.out.println( "**** End value string \"" + valueStr + "\"" ); }

      }

      }
      ---------- End of Java source ------------

      ---------- Start of output -----------

            • Begin value string "1"
              java.lang.StringIndexOutOfBoundsException: String index out of range: 1
              at java.lang.String.charAt(String.java:687)
              at
              org.jscience.physics.amount.AmountFormat$PlusMinusError.parse(AmountFormat.java:173)
              at
              org.jscience.physics.amount.AmountFormat$PlusMinusError.parse(AmountFormat.java:117)
              at javolution.text.TextFormat.parse(Unknown Source)
              at org.jscience.physics.amount.Amount.valueOf(Amount.java:243)
              at AmountValueOf.test(AmountValueOf.java:16)
              at AmountValueOf.main(AmountValueOf.java:6)
            • End value string "1"
            • Begin value string "1 "
              amount = "1 "
            • End value string "1 "
            • Begin value string "1.0"
              java.lang.StringIndexOutOfBoundsException: String index out of range: 3
              at java.lang.String.charAt(String.java:687)
              at
              org.jscience.physics.amount.AmountFormat$PlusMinusError.parse(AmountFormat.java:183)
              at
              org.jscience.physics.amount.AmountFormat$PlusMinusError.parse(AmountFormat.java:117)
              at javolution.text.TextFormat.parse(Unknown Source)
              at org.jscience.physics.amount.Amount.valueOf(Amount.java:243)
              at AmountValueOf.test(AmountValueOf.java:16)
              at AmountValueOf.main(AmountValueOf.java:8)
            • End value string "1.0"
            • Begin value string "1.0 "
              java.lang.StringIndexOutOfBoundsException: String index out of range: 4
              at java.lang.String.charAt(String.java:687)
              at
              org.jscience.physics.amount.AmountFormat$PlusMinusError.parse(AmountFormat.java:183)
              at
              org.jscience.physics.amount.AmountFormat$PlusMinusError.parse(AmountFormat.java:117)
              at javolution.text.TextFormat.parse(Unknown Source)
              at org.jscience.physics.amount.Amount.valueOf(Amount.java:243)
              at AmountValueOf.test(AmountValueOf.java:16)
              at AmountValueOf.main(AmountValueOf.java:9)
            • End value string "1.0 "
                              • End of output --------------

        Activity

        There are no comments yet on this issue.

          People

          • Assignee:
            jscience-issues
            Reporter:
            shanmann
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated: