Finding the type of number entered by the user I am making a fractions calculator that can calculate fractions, whole numbers and mixed fractions.
So there is a String fraction, which is the input.
Also, the user will enter the fraction in a specified format which is:

For normal fraction, &quot;a/b&quot; (a and b are integers)
For whole numbers, &quot;c&quot; (c is an integer)
For mixed fraction, &quot;c a/b&quot; (c is integer, a/b is the fraction separated by a space)

I want the program to check and find out which type of fraction the user has entered and then proceed. I'm using If-Else If-Else for this.
fun fractionalize(fraction: String): List&lt;Int&gt;{
        //Finds type of number (input) and converts to List&lt;Int&gt; with [numerator, denominator, whole number]
        var result: List&lt;Int&gt; = listOf(0,1,0)
        var numerator: Int = 0
        var denominator:Int = 1
        var whole_number:Int = 0
        try {
            if ((fraction.contains('/') &amp;&amp; fraction.indexOf('/') == fraction.lastIndexOf('/')) &amp;&amp; !fraction.contains(&quot; &quot;)) {
                //input is a regular fraction
                var fraction_string = fraction.split(&quot;/&quot;)
                numerator = fraction_string[0].toInt()
                denominator = fraction_string[1].toInt()
                result = listOf(numerator, denominator, 0)
            } else if (fraction.isDigitsOnly()) {
                //input is a whole number
                result = listOf(fraction.toInt(), 1, 0)
            } else if ((fraction.contains('/') &amp;&amp; fraction.indexOf('/') == fraction.lastIndexOf('/')) &amp;&amp; (fraction.contains(&quot; &quot;) &amp;&amp; fraction.indexOf(' ') == fraction.lastIndexOf(' '))) {
                //input is mixed fraction
                var wholeNumber_split = fraction.split(&quot; &quot;)
                whole_number = wholeNumber_split[0].toInt()
                numerator = wholeNumber_split[1].split(&quot;/&quot;)[0].toInt()
                numerator += whole_number * denominator
                denominator = wholeNumber_split[1].split(&quot;/&quot;)[1].toInt()
                result = listOf(numerator, denominator, 0)
            }
            else {
                Toast.makeText(applicationContext,
                &quot;The number you entered is not in a valid format.&quot;,
                Toast.LENGTH_LONG).show()
            }
            //simplifying input
            if (result[2] != 0){
                numerator += whole_number * denominator
                result = listOf(numerator, denominator, 0)
            }
            return result
        }
        catch (e: Exception){
            Toast.makeText(applicationContext,
                &quot;The number you entered is not in a valid format.&quot;,
                Toast.LENGTH_LONG).show()
            return result
        }
    }

Is this the best way I can do that or is there cleaner and more efficient code I can make (personally I think this is too messy and can be made more efficient)?
Your code has several areas where it can be improved.
The first is that you should separate the parsing code from the platform code (Toast). Kotlin has a great testing framework, in which it is easy to write &quot;parsing &quot;1 2/3&quot; should return listOf(1, 2, 3)&quot;. This testing code works best if you run it directly on your developing machine, and without any Toast or Activity.
The next thing is that you should not use List&lt;Int&gt; in places where you originally wanted to say Fraction or maybe, to be more specific, MixedFraction. The point is that listOf(1, 2, 3, 4, 5) is a valid list but not a valid fraction. By using a specifically constrained type you force the Kotlin compiler to check your code for you.
class Fraction(
    val int: Int,
    val num: Int,
    val den: Int
)

To make your parser extensible to expressions that are more complicated, you must not use contains. Instead, the best-practice way is to split the input into tokens first (often using regular expressions) and then to combine these tokens to expression trees (using a parser that only looks at the very next token to decide what to do next). The details of all of this are covered in any good book on compiler construction.
In your case, the tokens would be:

unsigned integer
fraction slash
unary minus sign

Note that there is no &quot;space&quot; token, as that is usually left out when defining an expression language.