2014-10-04 27 views
0

我想添加更多的命令到我的java程序我写在unix,但有问题传递参数。我刚才输入了unix命令之前的文本文件作为程序参数,它工作得很好,但是想要求输入。试图修复它在我自己的,但一点点新的Java从输入解析字符串作为参数问题

我有,

 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 

} 

所以我得到了这些错误

CPU.java:24:错误:不兼容的类型 int count = scanner.nextLine(); ^ 需要:整数 发现:字符串 CPU.java:27:错误:实测parseInt函数(INT) StartProcess是=新StartProcess(输入,的Integer.parseInt(计数))没有合适的方法; (实际参数int不能通过方法调用转换转换为字符串) 方法Integer.parseInt和正式参数列表的长度不同) 2错误

+0

编译错误似乎不是你粘贴的代码。根据我的代码应该编译好。 – Neo 2014-10-04 22:44:55

回答

0

count已经是整数 - 不需要解析可变

StartProcess being = new StartProcess(input, count); 
+0

谢谢,但只修复其中一个错误,我仍然得到这个errror java:24:错误:不兼容的类型 int count = scanner.nextLine(); ^ 必需:int 找到:字符串 1错误 – user3034262 2014-10-04 22:41:12

+0

这与您在第二个列表中的不同 - 将其切换回'int count = scanner.nextInt();' – Reimeus 2014-10-04 22:42:50

0

此方法nextLine返回一个字符串,而不是整数。

你可能想用它来代替。

int count = scanner.nextInt(); 
0

你有一个字符串输入,改为整数,它应该工作。

0

您正在重新发明车轮。改用任何标准库,例如Commons CLI