我有我的代碼從它的文本文件和子(變量名)來搜索字符串「INT」,但我對串(變量名)如何從「int a,b,c = 0,d;」子字符串(變量名稱)
像一些問題「INT A,B, c = 0,d;「
我想我的輸出是:
- 變量名是:一個
- 變量名是:乙
- 變量名是:c
- 變量名:d
我可以修改我的代碼嗎?
這是我的代碼。
//print variable name and position
int k4 = line.indexOf("=",k+1);
int k5 = line.indexOf(";", k+1);
int k6 = line.indexOf(",", k+1);
int cTemp1 = 0;
//Count whitespace before variable name, print variable name and length of variable name
if (k4 != -1){
String temp4 = line.substring(k+3, k4); //Substring variable name
if(k4 != -1){
int varName = line.indexOf(temp4.trim(), k+3);
String total_length = line.substring(k+3, varName);
int wPattern = total_length.length();
System.out.println(" - Whitespace before variable name \"" + temp4.trim() + "\" are: ," + wPattern);
}
System.out.println(" - Variable name type INTEGER at line: " + lineNumber + " is: "+temp4.trim());
System.out.print(" - Length of variable name \"" + temp4.trim() + "\" is: ," + temp4.trim().length() + "\n\n");
varLength_Integer1 += temp4.trim().length();
}
else if(k5 != -1){
String temp5 = line.substring(k+4, k5);
System.out.println(" - Variable name type INTEGER at line: " + lineNumber + " is: "+temp5.trim());
System.out.print(" - Length of variable name \"" + temp5.trim() + "\" is: ," + temp5.trim().length() + "\n\n");
varLength_Integer2 += temp5.trim().length();
}
else if(k6 != -1){
String temp6 = line.substring(k+4, k6);
System.out.println(" - Variable name type INTEGER at line: " + lineNumber + " is: "+temp6.trim());
System.out.print(" - Length of variable name \"" + temp6.trim() + "\" is: ," + temp6.trim().length() + "\n\n");
varLength_Integer3 += temp6.trim().length();
}
avgInteger += (varLength_Integer1 + varLength_Integer2 + varLength_Integer3)/(double)c3;
//END Count whitespace before variable name, print variable name and length of variable name
}
非常感謝。
是的,它的工作,但如果 「int a,a1,b,b1;」 我會在「String patternVar =」([A-Za-z _] *)中加上0-9「;」對嗎? 並且如果像這樣輸入 「int d [] = {31,28,31,30,31,30,31,31,30,31,30,31};」 出放是 - 變量是28 - 變量是31 - 變量是30 - 變量是31 - 變量是30 - 變量是31 - 變量是31 - 變量是30 - 變量是31 - 變量爲30 - 變量爲31 但是,我只想要「d」。 我該如何修改你的代碼? 非常感謝。 – Anonymous
@匿名等待只是在幾分鐘內更新我的代碼,它通常應該是patternVar =([A-Za-z _] *)([])?「但測試它 – Kordi
@匿名確定更新我的代碼,到 – Kordi