我在路径 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/