2017-08-10 81 views
-1

我是Java新手,正在努力解決我遇到的這個錯誤。基本上,這部分代碼掃描視頻和字幕文件,如果它沒有找到任何要求用戶輸入文件擴展名的信息,則繼續。當我初始化我的字符串時,「從內部類引用的局部變量必須是最終的或有效的最終的」

我爲此引入了一個While循環,所以首先聲明並初始化我的字符串變量videoExtension和subtitleExtension。現在我收到以下錯誤 -

"FileRename.java:33: error: local variables referenced from an inner class 
must be final or effectively final 
               if(extension.equals(".txt") || 
extension.equals(".mp4") || extension.equals(".mkv") || 
extension.equals(videoExtension)) { 

^ 
FileRename.java:53: error: local variables referenced from an inner class 
must be final or effectively final 
               if(extension.equals(".srt") || 
(extension.equals(subtitleExtension))) { 

^ 
2 errors" 

任何幫助或建議將不勝感激,代碼下面這一節 -

import java.io.File; 
import java.io.FilenameFilter; 
import java.io.Console; 

public class FileRename { 
public static void main (String[] args) { 
    Console console = System.console(); 
    boolean listEmpty = true; 
    String videoExtension = ""; 
    String subtitleExtension = ""; 
    File[] videoList = new File[1]; 
    File[] subtitleList = new File[1]; 
    String fileNameToKeep = console.readLine("Enter what you want to keep of 
the first video file name > "); 

    try { 
    //Create new file with folder path 
    File folder = new File (System.getProperty("user.dir")); 

    //Create filter for the video files 
    while (listEmpty = true) { 
    FilenameFilter videoFilter = new FilenameFilter() { 

       public boolean accept(File dir, String name) { 
        if(name.lastIndexOf('.')>0) { 

         // get last index for '.' char 
         int extensionIndex = name.lastIndexOf('.'); 

         // get extension 
         String extension = name.substring(extensionIndex); 

         // match path name extension 
         if(extension.equals(".txt") || extension.equals(".mp4") || extension.equals(".mkv") || extension.equals(videoExtension)) { 
         return true; 
         } 
        } 
        return false; 
       } 
      }; 

    FilenameFilter subtitleFilter = new FilenameFilter() { 

       public boolean accept(File dir, String name) { 
        if(name.lastIndexOf('.')>0) { 

         // get last index for '.' char 
         int extensionIndex = name.lastIndexOf('.'); 

         // get extension 
         String extension = name.substring(extensionIndex); 

         // match path name extension 
         if(extension.equals(".srt") || (extension.equals(subtitleExtension))) { 
         return true; 
         } 
        } 
        return false; 
       } 
      }; 
    //List the files 
    videoList = folder.listFiles(videoFilter); //Creates a list of all of the video files in the folder 
    subtitleList = folder.listFiles(subtitleFilter); //Creates a list of all the subtitle files in the folder 

    //If the list is empy it allows the user to enter the extension and search again. 
    if (videoList.length == 0) { 
     videoExtension = console.readLine("I couldn't find any video files, please enter the file extension of the files including the dot > "); 
    } else if (subtitleList.length == 0) { 
     subtitleExtension = console.readLine("I couldn't find any subtitle files, please enter the file extension of the files including the dot > "); 
    }else { 
     listEmpty = false; 
    } 
} 
+0

所以,這裏的教訓:A)做一些先前的研究 - 使用搜索引擎來查找錯誤消息。 B)下一次提出[mcve]。如果我可以複製/粘貼您的代碼以自己查看錯誤消息,我會在15分鐘前回復。爲什麼我想要這麼做:因爲你的代碼很難閱讀。我錯過了第二個if使用來自外部方法體的變量(不允許)的事實。所以:關注編寫乾淨的代碼。你在這裏有什麼......有很多問題。 – GhostCat

回答

1

您正在使用匿名類時,你喜歡你擴展的FilenameFilter()在這個代碼中。這個匿名類實際上是FileRename類的內部類,如果這些變量被定義爲'final'(即一旦變量設置爲不能更改),內部類只能訪問它們自己範圍之外的變量。

您應該能夠通過定義videoExtension和subtitleExtension是最終解決這一問題:

final String videoExtension = ""; 
final String subtitleExtension = ""; 

然而,這將導致問題與您的代碼的最後幾行,你允許用戶設置這些字段因爲如果它們是最終的,這將是不可能的。你應該重新思考你希望這個功能如何工作,或者你應該避免使用匿名類,並讓你自己的類擴展FilenameFilter並使用它們。

+0

非常歡迎。 – GhostCat

相關問題