简体   繁体   English

扫描仪类出现问题

[英]Trouble in Scanner class

I'm preparing a Project in JAVA for my Computer Applications Internal Assessment and I got an error in calling the method despite the class has no Syntax errors spotted. 我正在用JAVA为我的计算机应用程序内部评估准备一个项目,尽管该类没有发现语法错误,但在调用该方法时遇到了错误。 this is my code: 这是我的代码:

    package CommandPromptBrowser.GoogleWebsite;
    import java.util.*;
    class Commandprompt
    {
    Scanner sc=new Scanner(System.in);
    Searchbox se=new Searchbox();
    Title ti=new Title();
    Searchresults sr=new Searchresults();
    String cmd;
    String key;
    void commandBox()
    {
        System.out.println("***");
        cmd=sc.next();
        key=sc.next();
    }  
    boolean typeCommand()
    {
        if(cmd.equals("type>box"))
        {
            return(true);
        }
        else 
        {
            return(false);
        }
    }
    boolean clickCommand()
    {
        if(cmd.equals("click>button"))
        {
            if(key.equals(se.search))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        else
        {
            return(false);
        }
    }
    void commands()
    {
        boolean res;
        if(res=typeCommand())
        {
            se.searchBox(key);
            commandBox();
        }
        else if(res=clickCommand())
        {
            sr.resultScreen();
            commandBox();
        }
    }
}

If I call a method called Googleclient,I get the following error: 如果调用名为Googleclient的方法,则会出现以下错误:

java.lang.StackOverflowError:
null(in java.lang.String)

and I think that the trouble is due to the Scanner class object. 而且我认为麻烦是由于Scanner类对象引起的。 I would like to mention the code of the class Googleclient- 我想提一下Googleclient-类的代码

    package CommandPromptBrowser.GoogleWebsite;
public class Googleclient
{
    Title ti=new Title();
    Searchbox sea=new Searchbox();
    Commandprompt cp=new Commandprompt();
    public void clientRunner()
    {
        ti.welcomeScreenTitle();
        sea.emptySearchBox();
        cp.commandBox();
        cp.clickCommand();
        cp.typeCommand();
        cp.commands();
    }
}

Please reply as fast as possible.Please..... 请尽快回复。请.....

If "cmd" is null, this will fail (null hasn't equals method): 如果“ cmd”为null,这将失败(null没有等于方法):

if(cmd.equals("type>box"))

Try to replace it with: 尝试将其替换为:

if("type>box".equals(cmd))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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