2016-10-22 37 views
-1

我希望能夠找到已經檢查過的對象的變量名稱(如果已被調用)。然後將此變量名稱存儲在數組或其他形式的存儲器中供以後參考。這是未完成狀態的代碼。查找變量名稱並將其存儲以供日後參考

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(""); 
     } 
    } 

因爲我已經考慮了將近一週,我不知道如何去做這件事。

+0

不要存放變量名。相反,嘗試製作一個ArrayList,並將對象本身放在那裏。 –

+1

例如,您確實應該使用真正的解析器,使用antlr。 – MeBigFatGuy

+0

@MikiP除了我想存儲變量名稱,因爲我計劃在掃描後的下一個步驟,並找到所有可能的變量類型,我需要它找到,然後它會掃描並找到每行可能包含該對象類型的關鍵字,並查看它引用的變量是否在所有可能變量的列表中,如果不是,則表示它不在列表中。 –

回答

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 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;].*

相關問題