2013-12-18 82 views
-2

我使用ArrayList完成了一個簡單的字符串處理任務。現在我要做的是要求用戶輸入一個參數,然後處理該字符串(刪除元音,使所有的小寫字母等)。首先我硬編碼一個字符串以確保一切正常,但現在用戶必須告訴程序要處理的字符串(用戶可以編寫這個字符串:java StringProcessing input.txt)。我試圖編寫一個參數的代碼,但我收到錯誤消息。如何在java中使用參數

這裏是錯誤:

StringProcessing.java:9: error: illegal start of type 
} catch (Exception e) { 
^
StringProcessing.java:14: error: <identifier> expected 
    input.add(printStackTrace); 
      ^
StringProcessing.java:14: error: <identifier> expected 
    input.add(printStackTrace); 

代碼如下:

import java.util.ArrayList; 

    public class StringProcessing { 


    public static void main (String [] args) { 

      Scanner s = new Scanner(new File(args[0])); 
      } catch (Exception e) { 
      e.printStackTrace(); 
      } 

      ArrayList<String> input = new ArrayList<String>(); 
      input.add(printStackTrace); 

    //  input.add("ahHhdafsAhsopeifnlAshash339SNnadfnoaAhasdfhash3aash33hhnllashppeHhd3PPDDkashnbn4ijashonethelightalhnZZndaeJ"); 


      int index=0; 
      int numberOfTimes=0; 


       System.out.print(input.get(0).substring((input.get(0).length()/2),((input.get(0).length()+2)/2))); 

      System.out.println("\n"); 

      for (int i=0; i<input.get(0).length(); i+=index) { 
        index = input.get(0).indexOf("ash"); 
        if (index!=-1){ 
          numberOfTimes+=1; 
        } else { 
         break; 
       } 
      } 

    System.out.print(numberOfTimes); 
    System.out.println("\n"); 


    String vowel = input.get(0); 
    vowel = vowel.replace("a",""); 

    vowel = vowel.replace("e",""); 

    vowel = vowel.replace("i",""); 

    vowel = vowel.replace("o",""); 

    vowel = vowel.replace("u",""); 

    input.add(vowel); 




    String lower = input.get(0).toLowerCase(); 

    System.out.print(lower); 
    System.out.print(input.get(1)); 


    } 


    } 
+2

什麼錯誤信息? –

+1

你有一個catch塊在你的主要方法的頂部,但沒有嘗試塊.. – turbo

+1

你覺得'input.add(printStackTrace);'呢? – pamphlet

回答

1

你可能不小心刪除了在某些時候有些不對頭在你main方法

try { // missing 
    Scanner s = new Scanner(new File(args[0])); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
+0

請注意,這隻能解決編譯錯誤。您仍然會遇到與實際獲取用戶輸入有關的其他問題。您正在成功打開文件(使用參數,即:'args [0]'),但您需要讀取文件並將文件內容添加到您的'輸入'數組列表中。 – MxyL

相關問題