简体   繁体   中英

How to prevent my program from crashing due to a user's input

I am trying to implement an algorithm "recongnizng strings in a language "

L = {'w$w' : w is a possible empty string of characters other than $, w' = reverse(w)}

my problem is whenever i input anything without having $, it crashes on the while loop. what will be the best way to prevent it from crashing?

public boolean isInLanguage(String inputString)
{
        StackReferenceBased stack1 = new StackReferenceBased(); 
        StackReferenceBased stack2 = new StackReferenceBased(); 
        Object qItem;
        Object sItem;
        int index = 0; 

        if (inputString.length() == 0)
        {
            return false; // empty string not in L  
        }

        else if (inputString.length() == 1)
        {
            return true; 
        }

        **while (inputString.charAt(index) != '$')**
        { 
            // save the first half of the string
            stack1.push(inputString.charAt(index));

            ++index;
        }  

        // index points to '$' or its value > than inputString.length()
        while (index < inputString.length()-1)
        {
            // save the second half of the string
            ++index;
            stack2.push(inputString.charAt(index));
        } 

        do
        {
            // match the first half of the string with the second half
        if ((stack1.isEmpty() && !stack2.isEmpty()) ||(!stack1.isEmpty() && stack2.isEmpty()))
        {
            return false;
        }
        qItem = stack1.peek();
        sItem = stack2.peek();

        if (qItem != sItem)
        {
            return false;
        }

        if (!stack1.isEmpty())
        {
            stack1.pop();
        }

        if (!stack2.isEmpty())
        {
            stack2.pop();
        }

        }while (!stack1.isEmpty() || !stack2.isEmpty());


        if (stack1.isEmpty() && stack2.isEmpty())
        {
            return true;
        }
        else
        {
            return false; 
        }


}

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4 at java.lang.String.charAt(Unknown Source) at assignmnet5.StackReferenceBased.isInLanguage(StackReferenceBased.java:87) at assignmnet5.Question3.main(Question3.java:19)

this is my main:

public static void main(String[]args)
    {
        StackReferenceBased stack = new StackReferenceBased(); 

        String str;
         boolean bool;

         Scanner kb = new Scanner(System.in);
         System.out.println( "Enter a string to be checked by the algorithm : ");
         str = kb.next();

        **bool = stack.isInLanguage(str);**
        if (bool == true)
           System.out.println( "The string is in language");
        else 
            System.out.println("The string is not in language");
    }

It sounds like this might suffice:

    if (inputString == null || !inputString.contains("$")) {
        return false; // empty string not in L  
    }

possible issue is a null pointer exception, try to add this line in the top of your function

public boolean isInLanguage(String inputString)
{
    if(inputString == null){
        return false;
    }
...
...

complete your code if you still have crashes, you will need to provide the error you've got when you run the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM