我正在编写一个程序,它从文件中逐行读取代码,并打印“int”之后的所有内容。
例如:
int x;
int y;
打印出来:
x
y
这是我到目前为止编写的代码
BufferedReader br = new BufferedReader(new FileReader("C:\\example.txt"));
String line = null;
while((line = br.readLine()) != null)
{
StringTokenizer token = new StringTokenizer(line);
while(token.hasMoreTokens())
{
String s = null;
s = token.nextToken();
if(s.equals("int"))
{
System.out.println(token.nextToken());
}
}
}
但是,我想排除要打印的 int 类型的函数,例如本例中的“MyFunction”
x
z
MyFunction(int
y)
x,
y
输入文件:
int x = 137;
int z = 42;
int MyFunction(int x, int y) {
printf("%d,%d,%d\n", x, y, z);
{
int x, z;
z = y;
x = z;
{
int y = x;
{
printf("%d,%d,%d\n", x, y, z);
}
printf("%d,%d,%d\n", x, y, z);
}
printf("%d,%d,%d\n", x, y, z);
}
}
我对java还很陌生,所以请耐心等待。
最佳答案
试试这个。仅适用于与上述类似的输入。
while((line = br.readLine()) != null)
{
StringTokenizer token = new StringTokenizer(line);
while(token.hasMoreTokens())
{
String s = null;
String r = null;
s = token.nextToken();
if(token.hasMoreTokens()){ //if s was not last token get next one
r = token.nextToken();
}
if(s.equals("int") && r != null && r.length()<3)
{
System.out.println(r.substring(0, 1)); // just to print x instead of x,
}
}
}
关于java - 打印 int 类型的变量(不包括函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36350166/