我希望能夠找到已經檢查過的對象的變量名稱(如果已被調用)。然後將此變量名稱存儲在數組或其他形式的存儲器中供以後參考。這是未完成狀態的代碼。查找變量名稱並將其存儲以供日後參考
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("");
}
}
因爲我已經考慮了將近一週,我不知道如何去做這件事。
不要存放變量名。相反,嘗試製作一個ArrayList,並將對象本身放在那裏。 –
例如,您確實應該使用真正的解析器,使用antlr。 – MeBigFatGuy
@MikiP除了我想存儲變量名稱,因爲我計劃在掃描後的下一個步驟,並找到所有可能的變量類型,我需要它找到,然後它會掃描並找到每行可能包含該對象類型的關鍵字,並查看它引用的變量是否在所有可能變量的列表中,如果不是,則表示它不在列表中。 –