A program to recognize words made up from any combination of given three words
<p>I am trying to write a java program which takes in as input a string and checks if it is valid or not. The rules for deciding are:</p>

<p>1) The string is recognizable if and only if it contains the words "pi", "ka","chu" as its fragments, in any order repeated any number of times .
2) If it contains any other fragments( or subsequence), Then it is unrecognizable</p>

<p>For ex- "pikachu" consisits of "pi", "ka","chu"  only and so it is recognizable.
"kachupi" consists of "ka","chu","pi"  only and so it is recognizable.
"pipi" is also recognizable" as it contains "pi" two times.
"pich" is not recognizable as there is "ch" which is not one of the specified subsequences.</p>

<p>I am posting my java code below. But it's not working properly. Please check it and help.
Thanks in advance!</p>

<pre><code>import java.util.Scanner;

public class RecognisingWords {


    public static void main(String[] args) throws Exception 
    {

        Scanner inp= new Scanner(System.in);
        String str;
        int len;
        System.out.println("Enter the string to be tested:");
        str=inp.nextLine();
        System.out.println(str);

        while(str !=null)
        {

        if(str.startsWith("pi"))
        {
            len=str.length();
            str= str.substring(2,len);          
        }

        else if(str.startsWith("ka"))
        {
            len=str.length();
            str= str.substring(2,len);          
        }

        if(str.startsWith("chu"))
        {
            len=str.length();
            str= str.substring(3,len);          
        }

        else
        {
            System.out.println("Unrecognisable Sequence");
            break;
        }

        }

        if(str == null)
        {
            System.out.println("Recognisable Sequence");
        }


    }

}
</code></pre>
