2014-10-04 95 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