java - 打印 int 类型的变量(不包括函数)

标签 java token tokenize

我正在编写一个程序,它从文件中逐行读取代码,并打印“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/

相关文章:

java - 什么更好 : Key Listener or key Adapter?

api - 在 JWT 中设置角色是最佳实践吗?

c - C 中分割字符串最快的算法?

javascript - 寻找 Ace Editor 中的更改

java - Spring - Oauth2 客户端服务器。 NoSuchMethodError : javax. servlet.ServletContext.getVirtualServerName() 在 tomcat 中运行时

java - 使用TimeLine每隔一定秒触发一个void方法JavaFX

java - 发送 freemarker 模板电子邮件

java - 在P2P架构中使用超时

python - 查找已连接的 token

c# - 在 C# 中使用正则表达式拆分字符串上的标记