java 为什么编译时会出现重复的修饰符错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10286499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:23:11  来源:igfitidea点击:

Why am I getting a repeated modifier error when compiling?

javaaccess-modifiers

提问by dwwilson66

import java.util.*;
import java.io.*;
public final class FileListing2 
{
    public static void main(String... aArgs) throws FileNotFoundException 
    {
         File startingDirectory= new File(aArgs[0]);
         List<File> files = FileListing.getFileListing(startingDirectory);
         //print out all file names, in the the order of File.compareTo()
         for(File file : files )
         {
              System.out.println(file);
         }
    }
    static public List<File> getFileListing(File aStartingDir) throws FileNotFoundException 
    {
        validateDirectory(aStartingDir);
        List<File> result = getFileListingNoSort(aStartingDir);
        Collections.sort(result);
        return result;
    }
    static private List<File> getFileListingNoSort(File aStartingDir) throws FileNotFoundException 
    {
          List<File> result = new ArrayList<File>();
          File[] filesAndDirs = aStartingDir.listFiles();
          List<File> filesDirs = Arrays.asList(filesAndDirs);
          for(File file : filesDirs) 
          {
            result.add(file); //always add, even if directory
            if ( ! file.isFile() ) 
            {
                   //must be a directory
                   //recursive call!
                   List<File> deeperList = getFileListingNoSort(file);
                   result.addAll(deeperList);
            }
            return result;
          }

       }
       static private void validateDirectory (File aDirectory) throws FileNotFoundException 
       {
          if (aDirectory == null) 
          {
                 throw new IllegalArgumentException("Directory should not be null.");
          }
          if (!aDirectory.exists()) 
          {
                 throw new FileNotFoundException("Directory does not exist: " + aDirectory);
          }
          if (!aDirectory.isDirectory()) 
          {
                 throw new IllegalArgumentException("Is not a directory: " + aDirectory);
          }
          if (!aDirectory.canRead()) 
          {
                 throw new IllegalArgumentException("Directory cannot be read: " + aDirectory);
           }
        }
      }

I copied this code from this siteand am trying to adapt it for our environment. Unfortunately, I can't get the $%!@#*($ thing to compile. I keep getting an error at the main method: error: repeated modifier.

我从这个站点复制了这段代码,并试图使其适应我们的环境。不幸的是,我无法获得 $%!@#*($ 编译的东西。我在 main 方法中不断收到错误:错误:重复修饰符。

public static void main(String... aArgs) throws FileNotFoundExceptionis the line flagged for the error.

public static void main(String... aArgs) throws FileNotFoundException是标记为错误的行。

I don't see any duplicate modifiers in here, All my curly braces and parens appear to be in the right places, and I am completely stumped.

我在这里没有看到任何重复的修饰符,我所有的花括号和括号似乎都在正确的位置,我完全被难住了。

This is my first time working with varargs...I'm not sure if ~that's~ giving me an issue? I scanned Java Documentation, but didn't spot any red flags. Besides, it compiles just fine when I change String...to String[]. I feel I may get some null values, so I'd prefer to leave String...in the method.

这是我第一次使用可变参数...我不确定〜那〜是否给我带来了问题?我扫描了 Java 文档,但没有发现任何危险信号。此外,当我更改String...String[]. 我觉得我可能会得到一些空值,所以我宁愿留String...在方法中。

Anyone see anything that I'm missing? I feel like I'm looking at that Paris in the the spring brainteaser....

有人看到我遗漏的任何东西吗?我感觉我在看春天脑筋急转弯中的那个巴黎......

回答by óscar López

In the method getFileListingNoSort()the line return result;is outside of the method, move it one line up to put it inside, where it belongs.

在方法中,getFileListingNoSort()该行在return result;方法之外,将其向上移动一行以将其放入它所属的内部。

回答by Michael Schmei?er

I could not reproduce your repeated modifier error (using JDK 1.7.0), but the return statement of the method getFileListingNoSortis not in the method body.

我无法重现您重复的修饰符错误(使用 JDK 1.7.0),但方法的返回语句getFileListingNoSort不在方法体中。

Please format your code so that it makes sense and you will see such issues. The following compiles fine:

请格式化您的代码,使其有意义,您将看到此类问题。以下编译正常:

import java.util.*;
import java.io.*;

public final class FileListing2 {
    public static void main( String... aArgs ) throws FileNotFoundException {
        File startingDirectory = new File( aArgs[0] );
        List<File> files = FileListing2.getFileListing( startingDirectory );
        for(File file : files) {
            System.out.println( file );
        }
    }


    static public List<File> getFileListing( File aStartingDir ) throws FileNotFoundException {
        validateDirectory( aStartingDir );
        List<File> result = getFileListingNoSort( aStartingDir );
        Collections.sort( result );
        return result;
    }


    static private List<File> getFileListingNoSort( File aStartingDir ) throws FileNotFoundException {
        List<File> result = new ArrayList<File>();
        File[] filesAndDirs = aStartingDir.listFiles();
        List<File> filesDirs = Arrays.asList( filesAndDirs );
        for(File file : filesDirs) {
            result.add( file ); // always add, even if
                                        // directory
            if(!file.isFile()) {
                List<File> deeperList = getFileListingNoSort( file );
                result.addAll( deeperList );
            }
        }
        return result;
    }


    static private void validateDirectory( File aDirectory ) throws FileNotFoundException {
        if(aDirectory == null) {
            throw new IllegalArgumentException( "Directory should not be null." );
        }
        if(!aDirectory.exists()) {
            throw new FileNotFoundException( "Directory does not exist: " + aDirectory );
        }
        if(!aDirectory.isDirectory()) {
            throw new IllegalArgumentException( "Is not a directory: " + aDirectory );
        }
        if(!aDirectory.canRead()) {
            throw new IllegalArgumentException( "Directory cannot be read: " + aDirectory );
        }
    }
}