2

次のエラーメッセージが表示されますが、問題を理解できないようです。本当に助けていただければ幸いです。エラーメッセージは次のようになります。-

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関数を実装する部分には、***が含まれています。

私を助けてくれるかもしれない人に事前に感謝します。

4

1 に答える 1

4

試す

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

そのエラーが発生する理由は、その時点でbufを参照している間、がまだタイプであるためです。StringObject


追加のヒントとして、コンパイラによって表示されるエラーメッセージを理解するように努力する必要があります。以下を見てください。

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のタイプはであり、あなたの2番目の引数のタイプはjava.lang.String!!!です。

このようにして、ソースコード内の問題をすばやく特定し、迅速な修正を提案することができました。

コンパイラーからのエラーメッセージを処理できることは、あなたが身につけなければならない必須のスキルなので、これがあなたにとって教育的な経験になることを願っています。

于 2010-02-28T03:09:18.430 に答える