2015-03-25 60 views
0

我正在做一個任務,我必須查看一個文件並識別令牌。我想我正走在正確的軌道上。我大部分工作正常,但我無法識別特定字符,因此在識別令牌時我可以選擇忽略它。它的一個\n我已經得出這個結論,因爲當我在if語句中嘗試tokens.get(6).equals('\n')時,它會出現錯誤,因爲它不是\n我無法識別存儲在此數組索引值處的內容。該文本文件看起來像這樣爪哇識別令牌

編輯:我知道它\n因爲我做System.out.println("ddd" + tokens.get(6) +"dddd")輸出功率爲ddd則新行,並dddd

編輯2:我添加的System.out.println(」 unidenfified 「+詞);這是輸出enter image description here

read a 
read b 
c := a + b + 3 
write c 

輸出應該是這個

<read>, read 
<id>, a 
<read>, read 
<id>, b 
<id>, c 
<assign>, := 
<id>, a 
<add_op>, + 
<id>, b 
<add_op>, + 
<number>, 3 
<write>, write 
<id>, c 

我看起來就像這樣

<read>, read 
<error>, unidenfified 
<error>, unidenfified 
<assign>, := 
<id>, a 
<add_op>, + 
<id>, b 
<add_op>, + 
<error>, unidenfified 
<error>, unidenfified 

我不知道爲什麼會這樣。 我的代碼:

import java.util.ArrayList; 
import java.util.Scanner; 
import java.io.*; 
import java.lang.Character; 

public class Tokens { 

    public static void main(String[] args) throws IOException { 

     Scanner input = new Scanner(System.in); // Scanner for taking input from 
               // the user 

     String fileName; 
     System.out.println("Enter the name of the file."); 
     fileName = input.next(); 

     fileExists(fileName); // Checks to see if the file exists 

     ArrayList<Character> arrayOfTokens = new ArrayList<Character>(); 
     ArrayList<String> assembled = new ArrayList<String>(); 
     readToArray(arrayOfTokens, fileName); 
     assembled = assembleTokens(arrayOfTokens); 

     for(int i = 0; i < assembled.size(); i++) { 
      analyze(assembled.get(i)); 
     } 

    } 

    /* 
    * readToArray goes through a file and adds all its elements in individual 
    * character form. It is stored into an arraylist and it is then returned 
    * 
    * @param storeChar: This is an arraylist of characters that the characters 
    * will be saved into and then returned. 
    * 
    * @param fileName: The filename that you want to take the data from. 
    */ 
    private static ArrayList<Character> readToArray(
      ArrayList<Character> storeChar, String fileName) throws IOException { 
     /* 
     * Block of code to setup the fileInput stream to take in data from the 
     * file. Reads character by character and stores into an arraylist. int 
     * atChar: the current character the reader is at. Returns in int format 
     * (Need to be converted to character later on) int currentIndex: to add 
     * a character to an index. Increments until no more characters are left 
     */ 
     FileInputStream fileInput = new FileInputStream(fileName); 
     int atChar; 
     int currentIndex = 0; 

     /* 
     * Loop to go through and add the converted character from an int to the 
     * arraylist. Loops until atChar returns -1 which means no more 
     * characters in file. 
     */ 
     while ((atChar = fileInput.read()) != -1) { 
      storeChar.add(currentIndex, (char) (atChar)); 
      currentIndex++; 
     } 
     fileInput.close(); 

     return storeChar; 
    } 

    /* 
    * fileExists method makes sure the file the user enters exists in the 
    * system. If it doesn't then the program will terminate before any further 
    * code is executed. 
    * 
    * @param fileName: Takes in a string paramater of the file name that you 
    * want to if it exists. 
    */ 
    private static void fileExists(String fileName) { 

     boolean ifExists; // Boolean statement that will later be set to the 
          // value of whether the file exists or not 

     File file = new File(fileName); 
     ifExists = file.exists(); 

     if (ifExists == false) { 
      System.out 
        .println("Unable to find the file. Will now close the program."); 
      System.exit(0); 
     } 
    } 

    private static ArrayList<String> assembleTokens(ArrayList<Character> tokens) { 

     ArrayList<String> identified = new ArrayList<String>(); 
     int counter = 0; 
     String concatinated = ""; 

     while (counter < tokens.size()) { 
      if (!tokens.get(counter).equals(' ')) { 
       concatinated += tokens.get(counter); 
       counter++; 
      } else { 
       identified.add(concatinated); 
       concatinated = ""; 
       counter++; 
      } 
     } 

     return identified; 
    } 

    private static void analyze(String word) { 
     if(word.equals("read")) { 
      System.out.println("<read>, read"); 
     } else if(word.equals("write")) { 
      System.out.println("<write>, write"); 
     } else if(word.equals(":=")) { 
      System.out.println("<assign>, :="); 
     } else if(word.equals("(")) { 
      System.out.println("<lparen>, ("); 
     } else if(word.equals(")")) { 
      System.out.println("<rparen>,)"); 
     } else if(word.equals("+") || word.equals("-")) { 
      System.out.println("<add_op>, " + word); 
     } else if(word.equals("*") || word.equals("/") || word.equals("//") || word.equals("%")) { 
      System.out.println("<mult_op>, " + word); 
     } else if(word.matches("[a-z]+[A-Za-z0-9]*")) { 
      System.out.println("<id>, " + word); 
     } else if(word.matches("\\d+(\\.\\d+)?")) { 
      System.out.println("<number>, " + word); 
     } else { 
      System.out.println("<error>, unidenfified"); 
     } 

    } 
} 
+0

最快的方法是將調試它像'的System.out.println(」 ,unidenfified - 「+ word);' – 2015-03-25 05:47:50

+0

嘿謝謝,我忘了我之前做過。在我的輸出上的第一個錯誤,它說,未認證的一個 (在一個新的行)讀 (在一個新的行),unidenfified b (在一個新行)c清楚它的\ n但idk做什麼這裏... – user1881401 2015-03-25 05:49:11

回答

1

文件

if (atChar != '\n') { 
    storeChar.add(currentIndex, (char) (atChar)); 
} 

或者在閱讀時只需濾除\n你可以使用

fileInput.readLine() 

,然後記號化字符串

編輯

有了上次編輯後一看,如果會更好做readLine,然後使用String.split(" ");讓您的令牌

+0

我只是厭倦了,這裏沒有運氣是我的while/if statment「while((atChar = fileInput.read())!= -1){if(atChar!='\ n' ){ \t \t \t \t storeChar.add(CURRENTINDEX,(炭)(atChar)); \t \t \t \t CURRENTINDEX ++; \t \t \t} \t \t}「 – user1881401 2015-03-25 05:55:52

+0

'\ r \ n'也許?嘗試打印每個字符。這是非常基本的調試。 – 2015-03-25 05:58:14

+0

看看你最後一次編輯後,如果最好做一個'readLine'然後使用String。分裂(」 」);獲取您的令牌 – 2015-03-25 06:00:29