【问题标题】:trouble with StringTokenizerStringTokenizer 的问题
【发布时间】:2010-02-28 03:07:01
【问题描述】:

我收到以下错误消息,但我似乎无法找出问题所在。非常感谢任何帮助。错误信息如下:-

BaseStaInstance.java:68: 找不到符号

符号:构造函数 StringTokenizer(java.lang.Object,java.lang.String)

位置:类 java.util.StringTokenizer st = new StringTokenizer(buf,",");

                                  ^

这里,BaseStaInstance 是我的主要公共类。

实现这个StringTokenizer的类如下:-

类 ServerConnect 扩展线程 {

Socket skt;
int iProcessId, iInProcessId;
int iOwnTimeStamp, iInTimeStamp;
ServerConnect scnt = null;

ObjectOutputStream myOutput;
ObjectInputStream myInput;

ServerConnect(){}
ServerConnect(Socket connection, int iProcessNo) {
    this.skt = connection;
    this.iProcessId = iProcessNo;
}

public void run() {
    try {

        //initialize the object "scnt" using the parameterized constructor
        ServerConnect scnt = new ServerConnect(skt, iProcessId);
        myInput = new ObjectInputStream(skt.getInputStream());

        while(true) {
            try{
                iOwnTimeStamp = Global.iTimeStamp;

                Object buf = myInput.readObject();

                //if we got input, print it out and write a message back to the remote client...
                if(buf != null){
                    scnt.replyChoice(buf);
                }
            }catch(ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    } catch(IOException e) {
        e.printStackTrace();
    }
}

void replyChoice(Object buf){       
    try{
        String sDeferReply = "";
        myOutput = new ObjectOutputStream(skt.getOutputStream());

        //the place where the basestation reads the request from the other basestation
        System.out.println("Server read:[ "+buf+" ]");

        //extract out the process id and the timestamp from the incoming request
        buf = buf.toString();

        ***StringTokenizer st = new StringTokenizer(buf,",");***

        //skip the word request
        st.nextToken();
        iInProcessId = Integer.parseInt(st.nextToken());
        iInTimeStamp = Integer.parseInt(st.nextToken());

        //check request is made
        //there is a possibility of entering the else loop only on the very first iteration
        //the control flows into the if loop even if one request has been made
        if(iOwnTimeStamp != 0){
            //if the incoming request has a larger timestamp (logical clock value, process id) than the current process, we defer the reply
            if(iOwnTimeStamp < iInTimeStamp || iProcessId < iInProcessId){
                sDeferReply="iInTimeStamp"+","+"iInProcessId";
                Global.v.addElement(new String(sDeferReply));
            }
            //incoming request has a smaller timestamp than the basestation request itself
            else{
                myOutput.writeObject("Reply");
                myOutput.flush();
            }
        }
        //if the current process is in the critical section then we defer replies
        else if(Global.iCriticalSection==1){
            sDeferReply="iInTimeStamp"+","+"iInProcessId";
            Global.v.addElement(new String(sDeferReply));
        }
        //start of execution of the thread, there is a possibility that the basestation hasn't issued a request
        else{
            myOutput.writeObject("Reply");
            myOutput.flush();   
        }
    }catch(IOException e){
        e.printStackTrace();
    }
}

}

实现StringTokenizer函数的部分有***包围它。

提前感谢任何可以帮助我的人。

【问题讨论】:

    标签: java constructor stringtokenizer


    【解决方案1】:

    试试

    StringTokenizer st = new StringTokenizer((String) buf,",");
    

    您收到该错误的原因是,buf 在此时引用 String 时仍然是 Object 类型。


    作为附加提示,您确实应该努力尝试理解编译器给出的错误消息。请看以下内容:

    cannot find symbol constructor StringTokenizer(java.lang.Object,java.lang.String)
    location: class java.util.StringTokenizer st = new StringTokenizer(buf,",");
    

    编译器错误消息并不总是有意义,但这是最好的。它告诉你:

    • 它找到了正确的类型,java.util.StringTokenizer,所以它不是import 或名称模糊问题等。
    • 它告诉您找不到具有给定签名的特定方法。事实上,通过 API 快速检查确认 StringTokenizer 没有采用 (java.lang.Object, java.lang.String) 的构造函数。
    • 它会告诉您程序中试图调用这个不存在的方法的代码行确实,你的第一个参数的类型是java.lang.Object,你的第二个参数的类型是java.lang.String!!!

    这就是我能够快速查明源代码中的问题并提出快速修复建议的方式。

    能够处理编译器给出的错误消息是你必须培养的一项基本技能,所以我希望这对你来说是一种教育经验。

    【讨论】:

    • 嘿,谢谢你的帮助,它成功了。我实际上尝试使用 buf.toString() 将 buf 转换为字符串,但我将它存储回 buf,它是 object 类型的。
    • 很高兴为您提供帮助。而且由于您是新手,stackoverflow 的工作方式是,如果某人的回答有帮助,您就投票给他们声誉积分。这就是我答案左侧的箭头和检查点的用途。
    • 哦,好的。好吧,我尝试投票,但我不断收到一条消息,因为我需要 15 个声望点才有资格。 :)
    • 好吧,我只给了你 20 分。再试一次=)
    • 很抱歉我昨天没有投票。但我现在才这样做,我希望我做得对...:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    相关资源
    最近更新 更多