public static Fraction of(String str) {
        if (str == null) {
            throw new IllegalArgumentException("The string must not be null");
        }
        // parse double format
        int pos = str.indexOf('.');
        if (pos >= 0) {
            return of(Double.parseDouble(str));
        }

        // parse X Y/Z format
        pos = str.indexOf(' ');
        if (pos > 0) {
            final int whole = Integer.parseInt(str.substring(0, pos));
            str = str.substring(pos + 1);
            pos = str.indexOf('/');
            if (pos < 0) {
                throw new NumberFormatException("The fraction could not be parsed as the format X Y/Z");
            } else {
                final int numer = Integer.parseInt(str.substring(0, pos));
                final int denom = Integer.parseInt(str.substring(pos + 1));
                return of(whole, numer, denom);
            }
        }

        // parse Y/Z format
        pos = str.indexOf('/');
        if (pos < 0) {
            // simple whole number
            return of(Integer.parseInt(str), 1);
        } else {
            final int numer = Integer.parseInt(str.substring(0, pos));
            final int denom = Integer.parseInt(str.substring(pos + 1));
            return of(numer, denom);
        }
    }
public static Fraction of ( String str ) { if ( str == null ) { throw new IllegalArgumentException ( "The string must not be null" ) ; } int pos = str . indexOf ( '.' ) ; if ( pos >= 0 ) { return of ( Double . parseDouble ( str ) ) ; } pos = str . indexOf ( ' ' ) ; if ( pos > 0 ) { final int whole = Integer . parseInt ( str . substring ( 0 , pos ) ) ; str = str . substring ( pos + 1 ) ; pos = str . indexOf ( '/' ) ; if ( pos < 0 ) { throw new NumberFormatException ( "The fraction could not be parsed as the format X Y/Z" ) ; } else { final int numer = Integer . parseInt ( str . substring ( 0 , pos ) ) ; final int denom = Integer . parseInt ( str . substring ( pos + 1 ) ) ; return of ( whole , numer , denom ) ; } } pos = str . indexOf ( '/' ) ; if ( pos < 0 ) { return of ( Integer . parseInt ( str ) , 1 ) ; } else { final int numer = Integer . parseInt ( str . substring ( 0 , pos ) ) ; final int denom = Integer . parseInt ( str . substring ( pos + 1 ) ) ; return of ( numer , denom ) ; } }
<p>
Creates a Fraction from a <code>String</code>.
</p>

<p>
The formats accepted are:
</p>

<ol>
<li><code>double</code> String containing a dot</li>
<li>'X Y/Z'</li>
<li>'Y/Z'</li>
<li>'X' (a simple whole number)</li>
</ol>
<p>
and a .
</p>

@param str
the string to parse, must not be <code>null</code>
@return the new <code>Fraction</code> instance
@throws IllegalArgumentException
if the string is <code>null</code>
@throws NumberFormatException
if the number format is invalid