Java StringTokenizer的问题
我收到以下错误消息,我似乎无法解决问题。非常感谢您的帮助。错误消息的内容如下:- BaseStatance.java:68:找不到符号 符号:构造函数StringTokenizer(java.lang.Object,java.lang.String) 位置:类java.util.StringTokenizer st=新的StringTokenizer(buf,“,”) 在这里,BaseStatance是我的主要公共课 实现此StringTokenizer的类如下所示:- 类ServerConnect扩展线程{Java StringTokenizer的问题,java,constructor,stringtokenizer,Java,Constructor,Stringtokenizer,我收到以下错误消息,我似乎无法解决问题。非常感谢您的帮助。错误消息的内容如下:- BaseStatance.java:68:找不到符号 符号:构造函数StringTokenizer(java.lang.Object,java.lang.String) 位置:类java.util.StringTokenizer st=新的StringTokenizer(buf,“,”) 在这里,BaseStatance是我的主要公共课 实现此StringTokenizer的类如下所示:- 类ServerConne
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();
}
}
socketskt;
int iProcessId,iInProcessId;
int iOwnTimeStamp,iInTimeStamp;
ServerConnect scnt=null;
ObjectOutputStream myOutput;
ObjectInputStreamMyInput;
ServerConnect(){}
ServerConnect(套接字连接,int iProcessNo){
this.skt=连接;
this.iProcessId=iProcessNo;
}
公开募捐{
试一试{
//使用参数化构造函数初始化对象“scnt”
ServerConnect scnt=新的ServerConnect(skt,iProcessId);
myInput=newObjectInputStream(skt.getInputStream());
while(true){
试一试{
iOwnTimeStamp=Global.iTimeStamp;
Object buf=myInput.readObject();
//如果我们得到了输入,打印出来并写一条消息回远程客户端。。。
如果(buf!=null){
scnt.回复选择(buf);
}
}catch(classnotfounde异常){
e、 printStackTrace();
}
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
无效回复选择(对象buf){
试一试{
字符串sDeferReply=“”;
myOutput=newObjectOutputStream(skt.getOutputStream());
//基站从另一个基站读取请求的位置
System.out.println(“服务器读取:[“+buf+”]”);
//从传入请求中提取进程id和时间戳
buf=buf.toString();
***StringTokenizer st=新的StringTokenizer(buf,“,”)***
//跳过单词request
圣奈克特肯();
iInProcessId=Integer.parseInt(st.nextToken());
iInTimeStamp=Integer.parseInt(st.nextToken());
//提出检查请求
//只有在第一次迭代时才有可能进入else循环
//即使发出了一个请求,控制也会流入if循环
如果(iOwnTimeStamp!=0){
//如果传入的请求具有比当前进程更大的时间戳(逻辑时钟值、进程id),我们将延迟回复
if(iOwnTimeStamp
}
实现StringTokenizer函数的部分周围有***
提前感谢任何能够帮助我的人。试试看
StringTokenizer st = new StringTokenizer((String) buf,",");
出现该错误的原因是buf
,此时引用的字符串仍然是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转换为字符串,但我将其存储回了类型为object的buf中。很高兴提供帮助。因为你是新来的,stackoverflow的工作方式是,如果某人的答案是有帮助的,你投票给他们,给他们声誉分数。这就是我答案左边的箭头和检查点的作用。嗯,我试过投票,但我一直收到一条信息,因为我需要15个信誉点才能符合条件。:)好的,我刚刚给了你20分。对不起,我昨天没有投票。但我现在就这么做了,我希望我做对了…:)
cannot find symbol constructor StringTokenizer(java.lang.Object,java.lang.String)
location: class java.util.StringTokenizer st = new StringTokenizer(buf,",");