简体   繁体   English

StringTokenizer的麻烦

[英]trouble with StringTokenizer

I'm getting the following error message and I can't seem to figure out the problem. 我收到以下错误消息,但似乎无法找出问题所在。 Would really appreciate any help. 非常感谢您的帮助。 The error message reads as:- 错误消息显示为:-

BaseStaInstance.java:68: cannot find symbol BaseStaInstance.java:68:找不到符号

symbol : constructor StringTokenizer(java.lang.Object,java.lang.String) symbol:构造函数StringTokenizer(java.lang.Object,java.lang.String)

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

                                  ^

Here, BaseStaInstance is my main public class. 在这里,BaseStaInstance是我的主要公共类。

The class that implements this StringTokenizer is as follows:- 实现此StringTokenizer的类如下:-

class ServerConnect extends Thread { 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();
    }
}

} }

The part that implements the StringTokenizer function has *** surrounding it. 实现StringTokenizer函数的部分带有***。

Thanks in advance to anyone who might be able to help me out. 在此先感谢任何可能帮助我的人。

Try 尝试

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

The reason why you're getting that error is because buf , while referring to a String at that point, is still of type Object . 之所以会出现该错误,是因为buf在此时引用一个String时,其类型仍然是Object


As an additional tip, you really should make the effort to try to understand the error message given by the compiler. 另外,您确实应该努力尝试理解编译器给出的错误消息。 Look at the following: 请看以下内容:

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

Compiler error messages don't always make sense, but this is as good as it gets . 编译器错误消息并不总是很有意义,但这已经足够了 It tells you that: 它告诉您:

  • It found the right type, java.util.StringTokenizer , so it's not an import or name obscuring problem, etc. 它找到了正确的类型java.util.StringTokenizer ,因此它不是import或名称混淆的问题,等等。
  • It's telling you that a specific method with the given signature can not be found. 它告诉您找不到具有给定签名的特定方法。 Indeed, a quick check with the API confirms that StringTokenizer does NOT have a constructor that takes a (java.lang.Object, java.lang.String) . 确实,对API的快速检查确认StringTokenizer没有采用(java.lang.Object, java.lang.String)的构造函数。
  • It's telling you exactly the line of code in your program that tries to invoke this non-existent method. 它告诉您程序中试图调用此不存在的方法的确切代码行 And indeed , the type of your first argument is a java.lang.Object , and the type of your second argument is a java.lang.String !!! 确实 ,第一个参数的类型是java.lang.Object ,第二个参数的类型是java.lang.String

That was how I was able to quickly pinpoint the problem in the source code and suggest a quick fix. 这就是我能够快速查明源代码中的问题并提出快速修复的方法。

Being able to process error messages given by the compiler is an essential skill that you must develop, so I hope this proves to be an educational experience for you. 能够处理编译器给出的错误消息是您必须开发的一项基本技能 ,因此,我希望这对您来说是一种教育经验。

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

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