我使用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));
}
}
什麼錯誤信息? –
你有一個catch塊在你的主要方法的頂部,但沒有嘗試塊.. – turbo
你覺得'input.add(printStackTrace);'呢? – pamphlet