2015-12-08 91 views
1

我要開始說我已經看過一個叫做「我必須讓用戶輸入直到」完成「輸入的循環」我沒有運氣代碼在那裏給出答案。輸入用戶輸入,直到輸入<<EOF>>

我一直在考慮我的編輯命令的描述是這樣的:

「編輯一個文本文件,如果存在,否則,創建新的文本文件的命令等待用戶鍵入的文本(。必須支持多行文本),用戶通過輸入<<EOF>>並按回車鍵結束輸入。「

現在我的代碼是這樣的:

else if (spaceSplit[0].equals("edit")) { 
      String name = spaceSplit[1]; 
      boolean endOfFile = false; 
      String content = ""; 

      while(endOfFile == false){ 
       String userInput = s.next(); 
       content += userInput; 
       if(content.contains("<<EOF>>")){ 
        endOfFile = true; 
       } 
      } 

      FileSystem.edit(name, content); 


     } 

沒有什麼錯誤出,但我else語句打印。我else語句代碼是這樣的:

else { 
      System.out.println("That is not a command. Please try again."); 
     } 

什麼也時髦的是,該方案經過整個do while loop然後打印的東西。我知道這是因爲什麼恰好是打印:$That is not a commond. Please try again.

這裏是我的do while loop開頭:

do { 

     System.out.print("$"); 

     String input = s.nextLine(); 
     input = input.toLowerCase(); 
     spaceSplit = input.split(" "); 

相當混亂。另外我edit(String name, String content)功能如下:

public static void edit(String name, String content){ 
    for(int i = 0; i < texts.size(); i++){ 
     if(texts.get(i).getName().equals(name)){ 
      texts.get(i).setContent(content); 
     } else { 
      texts.add(new TextFile(name,content)); 

      for(int j = 0; j < directories.size(); j++){ 
       if(directories.get(j).getName().equals(wDir.getName())){ 
        texts.get(texts.size() - 1).setParent(directories.get(j)); 
        System.out.println("The parent of " + name + " is " + directories.get(j).getName()); 
       } 
      } 
     } 
    } 
} 

正如你可以看到我已經做了我edit(name,content)方法的一查到底,以檢查文件是否被正確地打印出的文本文件的父目錄中創建。

這是一次我所說的編輯命令我的程序應該如何發揮作用:

$mkdir d 
$cd d 
$edit stuff.txt 
Hello everyone, this is just an example!<<EOF>> 
The parent of stuff.txt is d 
$exit 
Good Bye! 

提供任何幫助,將不勝感激。

這裏是整個do while loop

do { 

     System.out.print("$"); 

     String input = s.nextLine(); 
     input = input.toLowerCase(); 
     spaceSplit = input.split(" "); 

     if (spaceSplit[0].equals("mkdir")) { 
      if (spaceSplit[1].equals("-p")) { 
       for (int i = 3; i < spaceSplit.length; i++) { 

       } 
      } else if (spaceSplit[1].contains("/")){ 
        //This method will create a directory farther down the tree like creating c in a/b/c 
        String[] dirSplit = spaceSplit[1].split("/"); 
        int length = dirSplit.length; 

        FileSystem.mkdir(dirSplit[length-1]); 
        int directoriesLength = FileSystem.directories.size(); 

        for(int i = 0; i < FileSystem.directories.size(); i++){ 
         if(dirSplit[length-2].equals(FileSystem.directories.get(i))){ 
          FileSystem.directories.get(i).addChild(FileSystem.directories.get(directoriesLength-1)); 
          //Checking if this works 
          System.out.println("The child was created succesfully"); 
         } 
        } 

      } else { 
       for (int i = 1; i < spaceSplit.length; i++) { 
        FileSystem.mkdir(spaceSplit[i]); 
       } 
      } 

     } else if (spaceSplit[0].equals("cd")) { 
      FileSystem.cd(spaceSplit[1]); 
     } else if (spaceSplit[0].equals("pwd")) { 
      FileSystem.pwd(); 
     } else if (spaceSplit[0].equals("ls")) { 

     } else if (spaceSplit[0].equals("edit")) { 
      String name = spaceSplit[1]; 
      boolean endOfFile = false; 
      String content = ""; 

      while(endOfFile == false){ 
       String userInput = s.next(); 
       content += userInput; 
       if(content.contains("<<EOF>>")){ 
        endOfFile = true; 
       } 
      } 

      FileSystem.edit(name, content); 


     } else if (spaceSplit[0].equals("cat")) { 
      for(int i = 1; i < spaceSplit.length; i++){ 
       FileSystem.cat(spaceSplit[i]); 
      } 
     } else if (spaceSplit[0].equals("updatedb")) { 

     } else if (spaceSplit[0].equals("locate")) { 

     } else if (spaceSplit[0].equals("exit")) { 
       exitProg = true; 
       System.out.println("Good bye!"); 
     } else { 
      System.out.println("That is not a command. Please try again."); 
     } 

    } while (exitProg == false); 
+1

請將整個do while循環顯示爲一個單獨的塊。不要亂序顯示部分。 –

+0

你從哪裏獲得用戶輸入?另外,您如何計劃從命令行運行此代碼?你不能只是說「編輯stuff.txt」,並讓它運行一些jar和執行操作 – redFIVE

+0

我看到你有一個for循環多數民衆贊成在做什麼,不應該在那裏 –

回答

0

好吧,嗯,我想我會在這裏回答我的問題。現在一切正常。

else if (spaceSplit[0].equals("edit")) { 
      if(spaceSplit.length > 1) { 
       String name = spaceSplit[1]; 
       boolean endOfFile = false; 
       String content = ""; 

       while (!(content.contains("<<EOF>>"))) { 
        String userInput = s.nextLine(); 
        content += userInput + " "; 
       } 

       String end = "<<EOF>>"; 

       content = content.replace(end, ""); 

       int size = tree.getTexts().size(); 

       if (size != 0) { 
        for (int i = 0; i < size; i++) { 
         if (tree.getTexts().get(i).getName().equals(name)) { 
          tree.getTexts().get(i).setContent(content); 
         } 
        } 
        tree.edit(name, content); 
       } else { 
        tree.edit(name, content); 
       } 
      } 

     } 
相關問題