public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new FileReader(args[1]));
String co = sc.next();
coup = Integer.parseInt(co);
当我尝试将 int 传递到命令行中的第二个参数时,出现 FileNotFoundException。这只是代码的一部分,文本文件作为 args[0] 传递。但是,我不知道如何传递一个简单的整数,只能传递文本文件。
最佳答案
public static void main(String[] args) throws Exception
{
Scanner scan = new Scanner(new FileReader(args[0]));
int integerFromCM = Integer.parseInt(args[1]);
}
您声明文本文件是第一个参数 (args[0]),因此在扫描器中分配该参数,并且在获取整数时,您需要做的就是将 args[1] 发送到 Integer.parseInt 方法中。您收到异常是因为您正在使用传入的整数的文件名分配 FileReader 对象。
关于Java 通过 Command 传递一个 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33661926/