尝试响应命令行用户输入的问题

时间:2015-11-01 18:39:55

标签: java output

我已经创建了这个类以及linkedlist类,它们位于同一个文件夹中。输入似乎工作正常,除了跑步者类不输出任何东西,即使我在命令提示符中输入p。即使我在System.out.print中封装了这些方法,也没有任何反应。

Runner class

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();
            }
    }

}

Linked List类

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;
}
}

1 个答案:

答案 0 :(得分:1)

我想这是因为你在这里吞下了几条输入线:

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