Java 查找变量名并将其存储以供以后参考
我希望能够找到一个对象的变量名,该对象已经过检查,如果它已经被调用。然后将此变量名存储在数组或其他形式的存储中,以供以后参考。这是处于未完成状态的代码Java 查找变量名并将其存储以供以后参考,java,variables,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);
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("");
}
}
因为我已经考虑了将近一周了,我不知道该怎么做。扫描.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编译器。请参阅此处的更多信息
话虽如此,您必须接受对您的方法的限制:代码必须“格式良好”以匹配您的搜索条件,否则将出现误报或错误匹配
- 假设包含“Scanner”的每一行实际上都是一个变量定义或声明李>
- Scanner后面的单词不是注释,我们假设它是变量名
- 每行仅定义一个扫描仪。(无
或扫描仪sA,sB;
)扫描仪sA;扫描仪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\u 0-9$]*)[\s;].
不存储变量名。相反,请尝试创建一个ArrayList,并将对象本身放在其中。您确实应该使用一个真正的解析器,例如使用antlr。@MikiP,但我希望按照我计划的方式存储变量名,在扫描后的下一步中,我会找到所有可能需要的变量类型,然后它将扫描并找到包含该对象类型的关键字短语的行的所有可能指示,并查看它引用的变量是否在所有可能变量的列表中,如果不在列表中,说它不在列表中。@MeBigFatGuy,除了这是一个荣誉的大学项目,它不需要使用外部框架。嘿,作为一个学生,我明白你的意思。但是使用antlr,您将学到比一些俗气的手工构建解析器多得多的东西。你的教授应该为他/她感到羞耻