Java 尝试响应命令行用户输入时出现的问题
我已经创建了这个类和linkedlist类,它们在同一个文件夹中。除了runner类不会输出任何内容之外,即使在命令提示符中输入p,输入似乎也可以正常工作。即使我将方法封装在System.out.print中,也不会发生任何事情 跑步班Java 尝试响应命令行用户输入时出现的问题,java,output,Java,Output,我已经创建了这个类和linkedlist类,它们在同一个文件夹中。除了runner类不会输出任何内容之外,即使在命令提示符中输入p,输入似乎也可以正常工作。即使我将方法封装在System.out.print中,也不会发生任何事情 跑步班 public class runner{ static String s; public static void main(String[] args) throws java.io.IOException{ linkedlis
public class runner{
static String s;
public static void main(String[] args) throws java.io.IOException{
linkedlist link= new linkedlist();
System.out.println("Type a command\n");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
try{
s=in.readLine();
char first=s.charAt(0);
while(in.readLine()!=null){
s=in.readLine();
int space= s.indexOf(" ");
if(first=='i'){
String w=s.substring(space);
link.insert(w);
}
if(first=='d'){
String w=s.substring(space);
link.delete(w);
}
if(first=='f'){
String w=s.substring(space);
link.find(w);
link.find(w);
}
if(first== 'p'){
link.printlist();
}
}
}catch(Exception e) {
System.out.println("Ack!: " + e);
}finally{
in.close();
}
}
}
链表类
import java.io.*;
public class linkedlist{
node head;
public linkedlist(){
head=null;
}
public void insert(String s){
head= new node(s,head);
}
public void printlist(){
node i= head;
while(i.getData(i)!=null){
System.out.print(i.getData(i));
i=i.getNext();
}
}
public String find(String s){
String comp=head.getData(head);
node ref=head.getNext();
String check=ref.getData(ref);
String temp=check;
while(check != "null"){
if(s.equals(check)){
head=ref;
ref=head.getNext();
check=ref.getData(ref);
}
else{
temp=check;
}
}
return temp;
}
public String delete(String s){
String comp=head.getData(head);
node ref=head.getNext();
String check= ref.getData(ref);
node ref2= ref.getNext();
String post=ref2.getData(ref2);
String temp=check;
while(check != "null"){
if(s.equals(check)){
ref=ref2;
}
else{
comp=check;
ref=head.getNext();
check=ref.getData(ref);
ref2= ref.getNext();
post=ref2.getData(ref2);
}
}
return temp;
}
}
我猜这是因为您在这里吞咽了几个输入行:
try{
s=in.readLine(); //you consume one line here
char first=s.charAt(0);
while(in.readLine()!=null){ //you consume another here
s=in.readLine(); //and another one..
替换为:
while ((s = in.readLine()) != null) { // while loop begins here
char first=s.charAt(0);
[...]
} // end while
您还应该发布
linkedlist
的代码,特别是insert方法。@hotzst我添加了整个链表Classis,这意味着您还有其他问题,但这里解决的问题肯定是个问题;这里的证据:哎哟,请忽略前面的链接,这是正确的:好的,谢谢。我想我知道你做了什么,但理论上,我的版本不应该和你的修改一起工作吗。除非我的linkedlist实际上没有存储任何我认为不属于这种情况的东西,否则在没有看到node类的情况下,很难说如何调用树来填充它;从修复控制台输入开始,然后转到其余部分