Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何防止我的程序因用户而崩溃';s输入_Java_Stack - Fatal编程技术网

Java 如何防止我的程序因用户而崩溃';s输入

Java 如何防止我的程序因用户而崩溃';s输入,java,stack,Java,Stack,我正在尝试实现一种算法“识别语言中的字符串” L={'w$w':w可能是除$,, w'=反向(w)} 我的问题是,每当我在没有$的情况下输入任何内容时,它就会在while循环中崩溃。防止它崩溃的最佳方法是什么 public boolean isInLanguage(String inputString) { StackReferenceBased stack1 = new StackReferenceBased(); StackReferenceBased st

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

L={'w$w':w可能是除$,, w'=反向(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; 
        }


}

可能的问题是空指针异常,请尝试在函数顶部添加此行

public boolean isInLanguage(String inputString)
{
    if(inputString == null){
        return false;
    }
...
...
完成你的代码
如果仍有崩溃,则需要提供运行代码时出现的错误。

听起来这样就足够了:

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

这是什么类型的碰撞?例外?张贴stacktrace,请/我已添加崩溃消息。而崩溃线正是我想要的**解决方案是仔细检查输入,绝不让用户输入导致你做违反应用程序规范的事情。这是StringIndexOutOfBoundsException。只有当输入中没有$while(inputString.charAt(index)!=“$”)时才会发生这种情况——是什么阻止了这种情况从字符串的末尾直接循环??线程“main”java.lang.StringIndexOutOfBoundsException中的异常:字符串索引超出范围:java.lang.string.charAt(未知源)处的4在assignmnet5.Question3.main(Question3.java:19)的assignmnet5.StackReferenceBased.isInLanguage(StackReferenceBased.java:87)中,它不是空异常。这是一个StringIndexOutOfBoundsException。只有当输入在itit中没有$it不是空异常时,才会发生这种情况。这是一个StringIndexOutOfBoundsException。只有当输入中没有$YES时才会发生。但检查“null”可能仍然有用。关键是,如果您真正想知道的是string.length()=0是否包含“$”的话,那么检查string.length()=0就是浪费时间。问:究竟是什么构成了“有效字符串”(这样您就不会因为试图获取不存在的子字符串而导致边界外错误)?如果可以避免的话,你不想一个字符一个字符地处理。你肯定不希望“索引”超过字符串长度:)我明白了。对不起,我没有注意到这个问题!inputString.contains部分
    if (inputString == null || !inputString.contains("$")) {
        return false; // empty string not in L  
    }