2010-02-28 27 views
2

我收到以下錯誤消息,我似乎無法找出問題所在。非常感謝任何幫助。錯誤消息全文: -StringTokenizer的問題

BaseStaInstance.java:68:找不到符號

符號:構造的StringTokenizer(java.lang.Object中,java.lang.String中)

位置:級Java。 util.StringTokenizer st = new StringTokenizer(buf,「,」);

       ^

這裏,BaseStaInstance是我主要的公共類。

實現此的StringTokenizer的類是如下: -

類ServerConnect擴展Thread {

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

嘗試

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 !!!

這就是我能夠快速查明源代碼中的問題並提出快速修復的方法。

能夠處理編譯器給出的錯誤消息是必須開發的基本技能,所以我希望這可以證明您是一個教育體驗。

+0

嘿,感謝您的幫助,它的工作。實際上我嘗試使用buf.toString()將buf轉換爲字符串,但是我將它存儲回buf中,該buf是object類型的。 – 2010-02-28 03:20:03

+0

很高興幫助。而且因爲你是新手,所以stackoverflow的工作方式是,如果某人的回答很有幫助,那麼你就可以投票給他們聲望點。這就是我答案左邊的箭頭和檢查點。 – polygenelubricants 2010-02-28 03:24:24

+0

哦,好的。好吧,我嘗試了投票,但我不斷收到一條消息,因爲我需要15點聲望才能符合條件。 :) – 2010-02-28 03:50:11