当我初始化String时,“从内部类引用的局部变量必须是最终的或有效的最终”

时间:2017-08-10 10:34:33

标签: java while-loop scope local-variables

我是Java的新手,正在努力解决我遇到的这个错误。基本上这部分代码扫描视频和字幕文件,如果它没有找到它询问用户文件扩展名,则继续。

我为此引入了一个While循环,因此首先声明并初始化我的String变量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;
    }
}

1 个答案:

答案 0 :(得分:1)

在扩展FilenameFilter()时使用的是匿名类,就像在此代码中一样。这个匿名类实际上是FileRename类的内部类,内部类只能访问自己范围之外的变量,如果这些变量被定义为' final' (即一旦变量被设置,它就不能改变)。

您应该可以通过将videoExtension和subtitleExtension定义为final来解决此问题:

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

但是,这会导致代码的最后几行出现问题,允许用户设置这些字段,因为如果它们是最终的,这将无法实现。您应该重新考虑如何使用此功能,或者您应该避免使用匿名类并创建自己的类来扩展FilenameFilter并使用它们。