java - 为什么我会收到此错误 'illegal start of type' ?

标签 java string compiler-errors syntax-error startup

我在它所说的部分(最终(String [] args))中收到错误。如果有人知道为什么会发生此错误,那就太好了。提前致谢!我已经包含了代码。

public class PartOne {


    private static Object BufferedReader;

    public static <String> void PartOne (final (String[] args))

    {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("words-sowpods.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
            String everything = sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            br.close();
        }
    }

最佳答案

这里有几件事不对-
公共(public)静态无效 PartOne (final (String[] args))

  • 为什么即使是 void
  • 也将泛型类型设为 String
  • 参数不正确

  • 请找到正确的实现:
     public static void PartOne(String[] args) {
            BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader("words-sowpods.txt"));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            try {
                StringBuilder sb = new StringBuilder();
                String line = br.readLine();
    
                while (line != null) {
                    sb.append(line);
                    sb.append(System.lineSeparator());
                    line = br.readLine();
                }
                String everything = sb.toString();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    

    关于java - 为什么我会收到此错误 'illegal start of type' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64108520/

    相关文章:

    java - jar 安全

    android - 未启动Android + Google API v2 +应用

    java - imageJ 宏中的字符串格式

    java - 在 DJigger 中搜索主机名没有找到任何内容

    c++ - char* str ="ab", str 和 &str 的混淆

    c# - 如何从元组字符串中选择最匹配的字符串

    c - Id 返回 1 退出状态错误

    javascript - 如何配置 create-react-kotlin-app 以使 kotlinx.serialization 正常工作

    java - 导出/导入加密代码 Java

    java - 需要将字符串的所有其他字符存储到另一个字符串中