java中无法解析字符串_关于java:从输入中解析字符串作为参数问题

我想给我在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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值