查找变量名称并将其存储以供日后参考

时间:2016-10-22 22:39:11

标签: java variables

我希望能够找到已经检查过的对象的变量名称。然后将此变量名称存储在数组或其他形式的存储中,以供日后参考。这是代码处于未完成状态。

    import java.util.*;

    public class errorHelpImproved
    {
     //Needs Scanner to detect what's in the line
       public static Scanner sc = new Scanner(System.in);
     //Create a section that reads line by line and finds commands that aren't imported
        public static void import1(File file)
        {
           /*logic note: 
              read line by line
              IF a line contains the keyword Scanner
                   -Set a flag to check for the keyphrase import java.util.Scanner; OR import java.util.*;
                   -If neither keyphrase found before the first instance of a keyword public class
                       +Then Scanner has been called without the package being imported
                   -If either keyphrase found, stop searching and add this Scanner variable to an array of Scanner variables
           */
          File fileStart = new File(file);
          do
          {
             String line = file.nextLine();
             if(line.contains(" Scanner "))
             {
                 boolean flagTest=false;
                 String line2;
                 do
                 {
                    line2 = fileStart.nextLine;
                    if(line2.contains("import java.util.*;") || line2.contains("import java.util.Scanner;"))
                    {   
                       flagTest=true;
                       break;
                    }
                 }while(!line2.contains("public class"))
                 if(flagTest == false)
                 {
                      System.out.println("Scanner class has been called without being imported.");
                 }
                 else if(flagTest == true)
                 {
                   //This is where it would find the word after scanner and store it
                 }
              }
          }while(file.hasNext());
    }
        //Main method gets name of file and passes it to the first check and then from there all other codes will use it
        public static void main(Strings [] args)
        {
            System.out.println("");
        }
    }

因为我已经考虑了将近一个星期,我不知道我会怎么做。

1 个答案:

答案 0 :(得分:0)

扫描.java文件以进行声明并读取变量名称比这更复杂。你可以这样做,但java代码不需要换行。一个java应用程序可以写在一行中。

添加换行符也是如此。您可以在代码中允许空格的位置添加换行符。

Scanner sVar;
Scanner
    sVar2;

两者都是合法的java代码。您的方法也将匹配文本文字和注释:

/* This is a line with the Scanner variable */ 
String value = "I am fooling the Scanner code with this line and the comment above";

阅读上面的评论:java编译器会执行老师要求的操作。您可以使用javax.tools包运行java编译器。请在此处查看更多信息http://docs.oracle.com/javase/8/docs/api/javax/tools/ToolProvider.html#getSystemJavaCompiler()

话虽如此,您必须接受对您的方法的限制:代码必须“格式良好”以符合您的搜索条件,否则您将出现误报或错误匹配。

  • 认为包含“Scanner”的每一行实际上都是变量定义或声明。
  • Scanner之后的单词不是注释,我们认为它是变量名。
  • 每行只定义一个扫描仪。 (否Scanner sA, sB;Scanner sA; Scanner sB;
  • 此外,您将匹配列表存储在列表中以供稍后处理(写入文件)。

然后丢失的代码可能如下所示:

else if(flagTest == true)
{
    //This is where it would find the word after scanner and store it
    int pos = line.indexOf("Scanner") + "Scanner ".length();
    String varStart = line.substring(pos);
    pos = varStart.indexOf(";");
    String varName = varStart.substring(0, pos).trim();
    variableNames.add(varName);
}

如果你在行上使用正则表达式匹配器运行它,那么限制性会更小,它具有变量名称的匹配组。但我认为这可能会让你的编码水平更加混乱。

具有匹配组的正则表达式应如下所示:.*Scanner\s+([a-zA-Z_$][a-zA-Z_0-9$]*)[\s;].*