【问题标题】:Return array initialized inside try/catch返回在 try/catch 中初始化的数组
【发布时间】:2021-05-21 06:32:48
【问题描述】:

我正在创建一个程序,它创建将文件读取到数组中,将哪个文件分隔到数组中的不同索引值中。

static String[] readFile () {
    int count = 0;
    
    try {
        File file = new File("input.txt"); // create file
        Scanner scanner = new Scanner(file); // create scanner associated to file
        
//          counts number of lines
        while (scanner.hasNextLine()) {
            scanner.nextLine();
            count++;
        }
        
//          reads file into array
        while (scanner.hasNextLine()) {
            String[] data = new String[count];
            int len = data.length;
            
            for (int i = 0; i <= len; i++) {
                data[i] = scanner.nextLine();
            }
        }
    } catch (Exception e) {
        System.out.println("File not found!!!");
        System.exit(0);
    }
    
    return data;
}

问题是,当尝试返回变量数据时,我收到一条错误消息“无法解析符号数据”,因为它是在 try-catch 块中初始化的。我尝试过 this 但它返回值 null 因为变量的长度由变量 count 确定,其值也在 catch 块中确定。提前致谢!

【问题讨论】:

  • 您链接的帖子这里的正确答案。您的问题不在于返回数组。您正在错误地读取文件。第一个循环已经读取了整个文件,所以第二个循环没有什么要读取的了。您需要关闭并重新打开文件,或使用ArrayList,这样您就不需要计算行数。

标签: java arrays try-catch


【解决方案1】:

您可以使用来自 cmets 的 @Sweeper 建议。会是这个样子。

        static ArrayList<String> readFile () {
            ArrayList<String> data = new ArrayList<>();
            try {
                File file = new File("input.txt"); // create file
                Scanner scanner = new Scanner(file); // create scanner associated to file
    
                while (scanner.hasNextLine()) {
                    data.add(scanner.nextLine()) ;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return data;
        }

但是如果你想保留你当前的代码,你可以在 try 块之外通过 null 来初始化 data。而且您还需要重置扫描仪。您的代码将看起来像这样。请注意,在for 循环中,您必须使用条件&lt;len 而不是&lt;=len

    static String[] readFile () {
        String[] data = null;
        try {
            File file = new File("input.txt"); // create file
            Scanner scanner = new Scanner(file); // create scanner associated to file

//          counts number of lines
            int count = 0;
            while (scanner.hasNextLine()) {
                scanner.nextLine();
                count++;
            }
            scanner.close(); // don't forget about closing resources
            
            data = new String[count];

//          reads file into array
            scanner = new Scanner(file);   
            for (int i = 0; i < len; i++) {
                data[i] = scanner.nextLine();
            }
            scanner.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

【讨论】:

  • 我试过了,现在错误消失了,但是当我这样做System.out.println(Arrays.toString(data));时,输出仍然为空
【解决方案2】:

以下是一些类似问题的答案:
Java: Reading a file into an array
Read text file into an array

另外,我想指出 try-with-resources 语句 - Scanner 对象应该在其中关闭或初始化。

此外,System.exit(0); 不是停止方法的好方法,因为在这种情况下不会执行它周围的所有 finally 块。

【讨论】:

    【解决方案3】:

    你有两个问题首先发生,因为变量数据是在 try-catch 块中声明的 ¿如果指令抛出异常并且从未声明变量数据怎么办?在这种情况下 ¿ 将返回什么?,解决方案是在 try-catch 块之前声明变量数据,第二个发生是因为当您调用 nextLine() 时,Scanner 对象保持其状态,因此当您尝试调用 nextLine () 再次遍历最后一行(没有下一行)的整个文件后,您可以调用 close() 解决它,然后再次初始化扫描仪对象,这将重置状态:

    static String[] readFile () {
            Scanner scanner = null;
            int count = 0;
            String[] data = null;
    
            try {
                File file = new File("C:\\Users\\Mulé\\Desktop\\doc.txt"); // create file
                scanner = new Scanner(file); // create scanner associated to file
    
    
    //          counts number of lines
                while (scanner.hasNextLine()) {
                    scanner.nextLine();
                    count++;
                }
                scanner.close();
                scanner = new Scanner(file);
    //          reads file into array
                data = new String[count];
    
                for (int i = 0; i < data.length; i++) {
    
                    data[i] = scanner.nextLine();
                }
    
            } catch (Exception e) {
                System.out.println("File not found!!!");
                System.exit(0);
            }
    
            return data;
        }
    

    【讨论】:

    • 如果我尝试声明一个没有长度的数组,它会给我一个错误,我不能只输入任何长度,因为长度只能声明一次,这就是我使用 null 和稍后在代码中声明它。
    • 你是对的,我没有分析你的代码,我只阅读了问题,这是解决方案
    猜你喜欢
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 2011-11-04
    • 1970-01-01
    • 2012-09-23
    相关资源
    最近更新 更多