他のJavaファイルを通り、クラス名、int変数名、およびコメントを表示できるクラスを作成する助けが必要です。別のファイルのクラス名、Int変数、およびコメントの出力
私はここで解析しようとしているテストクラスを持っています。
public class Test {
private int x;
private int y;
private String s;
public static void main(String[] args) {
// TODO Auto-generated method stub
// more comments
int l; //local variable
l = 0;
}
}
私が取得しているよ出力:
The Class name is : Test
There is an int variable named: x
There is an int variable named: y
Comment contains: TODO Auto-generated method stub
Comment contains: more comments
There is an int variable named: l
Comment contains: local variable
ここでは、私が今持っているクラスのコードです:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
class ExtractJavaParts {
public static void main(String args[]){
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("src/Test.Java");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null){
// Print the content on the console
if (strLine.contains("class")){
System.out.println ("The class name is: " + strLine.substring(strLine.indexOf("class ") + 6, strLine.indexOf("{")));
}
else if (strLine.contains("int")){
System.out.println("There is an int variable named: " + strLine.substring(strLine.indexOf("int ") + 4, strLine.indexOf(";")));
}
else if (strLine.contains("//")){
System.out.println("Comment contains: " + strLine.substring(strLine.indexOf("//") + 2));
}
}
//Close the input stream
in.close();
}
catch (Exception e){
//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
これは、現在出力されます。
The class name is: Test
There is an int variable named: x
There is an int variable named: y
Comment contains: TODO Auto-generated method stub
Comment contains: more comments
There is an int variable named: l
今のところプログラムはpコードの後に発生するコメントを書き留めてください。あなたが望む出力を得るために提供できるあらゆる助けは大いに感謝されます。本当にありがとう!
より多くのオプションを--has。何が簡単な修正。どうもありがとうございます! – kmaz13