【问题标题】:Why am I getting a repeated modifier error when compiling?为什么编译时出现重复的修饰符错误?
【发布时间】:2012-04-23 18:55:58
【问题描述】:
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);
           }
        }
      }

我从this site 复制了这段代码,并试图将其调整为适合我们的环境。不幸的是,我无法编译 $%!@#*($ 东西。我在 main 方法中不断收到错误:错误:重复修饰符。

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

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

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

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

【问题讨论】:

  • 你能指出哪一行给你编译问题吗?
  • main(String... aArgs java支持椭圆吗?
  • 您的代码的格式化方式既不常见,也不会使错误容易被发现。请考虑使用通用代码格式化程序(大多数 IDE 都集成了一个)。
  • 第 8 行需要更改为 FileListing2.getFileListing(startingDirectory) 然后您需要添加/修复返回到 getFileListingNoSort。然后它应该编译。编辑:重复奥斯卡在下面所说的话。

标签: java access-modifiers


【解决方案1】:

在方法getFileListingNoSort() 中,return result; 行在方法之外,将其上移一行以将其放入它所属的位置。

【讨论】:

  • @dwwilson66 相同的错误:return result; 在更新的代码中放错了位置;它应该在方法的最后一行,就在结束}之前。此外,在main() 方法的第二行中,使用FileListing2 而不是FileListing
【解决方案2】:

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

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

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 );
        }
    }
}

【讨论】:

  • 我剪切/粘贴了您的代码,将文件名调整为 FileListing3,以免覆盖旧版本。同样的错误:C:\Users\x46332\Desktop\dev\java\bin&gt;javac FileListing3.java.\FileListing.java:22: error: repeated modifierpublic static void main(String[] args) throws FileNotFoundException`^`我正在运行java版本“1.7.0_02”
猜你喜欢
  • 1970-01-01
  • 2014-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-10
  • 2010-11-04
  • 2023-03-30
相关资源
最近更新 更多