java - 如何防止我的程序因用户输入而崩溃

标签 java stack

我正在尝试实现一种算法“识别语言中的字符串”

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

我的问题是每当我输入任何没有 $ 的内容时,它都会在 while 循环中崩溃。防止其崩溃的最佳方法是什么?

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)

这是我的主要内容:

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");
    }

最佳答案

听起来这可能就足够了:

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

关于java - 如何防止我的程序因用户输入而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19351860/

相关文章:

java - 仅具有局部变量的实例方法的线程安全

java - 暂时禁用 Spring Security (ldap auth)

java - Apache Storm 拓扑上的 Sigar UnsatisfiedLinkError

c - 使用 Visual Studio 2010 在 C 中返回失败类型

c - 我要打印什么地址?

java - 数组 链表 栈

c - 数据结构 : Stack

java - 如何用sql代码将图片文件保存到mysql,用java怎么显示?

java - 带有 ADT 插件的 Eclipse - 布局编辑器不显示内容(卡在 "Loading editor"上)

c - 在 C 中从堆栈中复制数据