我想给我在UNIX中编写的Java程序增加更多的命令,但是有问题传递参数。我刚刚在Unix中输入了之前的命令,并将文本文件作为一个程序参数,这个参数工作得很好,但是我想请求输入。试图解决它自己,但有点新爪哇
我有,
public static void main(String[] args)
{
String input = args[0];
///
String count = args[1];
File newF = new File(input); //
StartProcess start = new StartProcess(input, Integer.parseInt(count)); //begin
}
它工作得很好,然后尝试了这个,但失败了
public static void main(String[] args)
{
//String inputFile = args[0]; //read the input file
Scanner scanner = new Scanner(new InputStreamReader(System.in));
System.out.println("Please enter file");
String input = scanner.nextLine();
System.out.println("Please enter number");
int count = scanner.nextInt();
//String interruptCounter = args[1]; //read the interrupt value
File newFile = new File(input); //create a new file with the input file
StartProcess being = new StartProcess(input, Integer.parseInt(count)); //begin
}
号
所以我有这些错误
Java: 24:错误:不兼容类型int count=scanner.nextline();^必需:int找到:字符串Java: 27:错误:对于PARSETINT(INT)没有找到合适的方法startprocess being=new startprocess(input,integer.parseint(count));//开始运行进程^方法integer.parseint(string)不适用(无法通过方法调用转换将实际参数int转换为字符串)方法integer.parseint(string,int)不适用(实际参数列表和正式参数列表的长度不同)2个错误
编译错误似乎不是针对您粘贴的代码。我所说的代码应该编译得很好。
你在重新发明轮子。使用任何标准库,例如commons cli。
您将输入作为一个字符串,更改为整数,它应该可以工作。
此方法返回字符串而不是整数。
你可能想用这个来代替。
int count = scanner.nextInt();
号
count已经是一个整数-不需要解析变量
StartProcess being = new StartProcess(input, count);
谢谢,但是只有固定的一个错误,我仍然得到这个Error Java:24:错误:不兼容类型int计数=扫描器.NestLINK();^需要:int发现:字符串1错误
这与您在第二个列表中的不同-将其切换回int count = scanner.nextInt();