java - 使用扫描仪将字符串存储在数组中

标签 java

我在路径 d:\input.txt 中有一个文件 我想从具有 Scanner 类的用户那里获取此路径 这是我的代码,但我收到 ArrayIndexOutOfBoundException 为什么?

if(args.length != 0)
        {
            readerWriter.readFile(args[0]);
        }
        else
        {
            System.out.println("Plese enter the path of your file");                
            Scanner filePath = new Scanner(System.in);
            args[0] = filePath.next();
            readerWriter.readFile(args[0]);
        }

最佳答案

您不能在此处使用数组 args[0] = filePath.next(); 因为您的数组未初始化并给出 ArrayIndexOutOfBoundException,请尝试:

字符串文件 = filePath.next(); readerWriter.readFile(文件);

关于java - 使用扫描仪将字符串存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32032752/

相关文章:

java.lang.NoClassDefFoundError : org/opencv/core/Core - Java Servlet + OpenCV

java - 包装类的工作原理

java - 计算机图形运算矩阵

java - 使用 SpringSecurity 和 Mock 进行 Spring Boot 单元测试

java - 如何获取 <tr> 行中 <td> 或 <th> 的列数?

java - 表达式预期错误

java - 将已安装的小部件添加到 Android Activity

java - "Element not found in the cache - perhaps the page has changed since it was looked up"显示

java - 应该执行复数算术的简单应用程序中出现编译错误

java - 将 setText 用于 EditText 框,onClick 认为它为空