2015-11-01 32 views
1

我已經創建了這個類以及鏈表類,它們位於同一個文件夾中。除了runner類不會輸出任何內容外,輸入似乎還可以正常工作,即使我在命令提示符中輸入p時也是如此。即使我將這些方法封裝在System.out.print中,也沒有任何反應。試圖響應命令行用戶輸入的問題

亞軍類

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

您還應該發佈'linkedlist'的代碼,特別是插入方法。 – hotzst

+0

@hotzst我添加了整個鏈表類 – Haukka

回答

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 
+0

不幸的是,沒有工作 – Haukka

+0

這意味着你有其他問題,但這裏解決的一個_IS_絕對是一個問題;在這裏證明:http://ideone.com/z1o8X9 –

+0

哎呦,請忽略以前的鏈接,這是正確的:http://ideone.com/LPA0ms –