2013-05-16 90 views
-3

代碼應該讀取文件並檢查它。如果數據類型或關鍵字或操作。代碼編譯沒有錯誤,但jTextArea不顯示檢查的結果。可能是我忘了一些東西,但jTextArea不讀任何東西。Java:讀取令牌

public boolean IsKeyWords(String x) 
{ 
    List myList = new ArrayList(); 
String[] keywords= new String[] { "for" , "if" , "else" , "return" ,"and" , "goto" , "true" ,"false" , "break" , "continue" , "do" , "this" , "class" , "delete" , "void" , "public" , "private" , "operator" , "static" , "struct" , "volatile" , "while" ,}; 
Collections.addAll(myList, keywords); 
    for(int i = 0;i<myList.size();++i) 
    {if(x.equals(myList.get(i))) 
     return true; 

    } 
    return false; 
}  
public boolean IsOperaions(String x) 
{ 
    List myList1 = new ArrayList(); 
String[] Operations = new String[]{"+", "-" , "*" , "%" , "/" , "=" , ">>=" , "<<=" , "&=" , "+=" , "-=" , "/=" , "*=" , "^=" , "++" , "--" , "==" , "!=" , ">" , "<" , ">=" , "<=" , "!" , "&&" , "||" , "|=" , ">>" , "<<" , "&" , "~" , "^" , "|"}; 
Collections.addAll(myList1, Operations); 
    for(int i = 0;i<myList1.size();++i) 
    {if(x.equals(myList1.get(i))) 
     return true; 

    } 
    return false; 

} 

public boolean IsDatatype(String x) 
{ 
    List myList2 = new ArrayList(); 
    String[] Datatype = new String[]{"int" , "char" ,"float" , "double" , "String"}; 
    Collections.addAll(myList2, Datatype); 
    for(int i = 0;i<myList2.size();++i) 
    {if(x.equals(myList2.get(i))) 
     return true; 

    } 
    return false; 

} 

這裏的按鈕,當我按下應讀取文件並檢查它。

private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {           


    JFileChooser chooser = new JFileChooser(); 
    chooser.showOpenDialog(null); 
    File f =chooser.getSelectedFile(); 
    String filename2=f.getAbsolutePath(); 
    BufferedReader br; 
    try { 
     br = new BufferedReader(new FileReader(filename2)); 
    String line; 
    ArrayList <String> tokens=new ArrayList<>(); 
    StringTokenizer words; 
    while ((line = br.readLine()) != null) { 
    words = new StringTokenizer(line); 

    while(words.hasMoreTokens()) 
     { 
     for(int i=0;i<line.length();++i) 
     { 
     tokens.add(line.getBytes().toString()); 

     } 
    words.nextToken(); 

     } 
     for(int i=0;i<tokens.size();i++) 
    { 
     jTextArea1.setText(tokens.get(i).toString()); 

    if(IsKeyWords(tokens.get(i))) 
    { 
    jTextArea1.setText(tokens.get(i).toString()); 
    } 
    else if(IsOperaions(tokens.get(i))) 
    { 
    jTextArea1.setText(tokens.get(i).toString()); 


    }} 
    br.close(); 

    } 
    } 
     catch (FileNotFoundException ex) { 
    } catch (IOException ex) { 
    }  

    }           
+3

'if(x == myList.get(i))'這不是我們如何比較Java中的字符串。這個問題今天被問了兩次:_( – Maroun

+0

但是當我使用時,我得到錯誤 –

+0

仍然我有同樣的問題不要讀任何東西:( –

回答

0

除了String#equalsString比較問題已經得到了很好的標記,主要的問題是,BufferedReaderbr被讀取循環試圖調用BufferedReader#readLine時拋出IOException內關閉。移動close語句到finally

} finally { 
    try { 
     br.close(); 
    } catch (IOException e1) { 
     e1.printStackTrace(); 
    } 
} 

另外,添加一個字節數組給令牌List將導致在JTextArea不可讀工作。更換

tokens.add(line.getBytes().toString()); 

在Java

tokens.add(line); 

集合使用泛型避免鑄造。將它們應用於List的所有實例,例如

List<String> myList = new ArrayList<>(); 
+0

它現在在TextArea中給了我一些東西,但它在文件中不是相同的「[B @ 752719ad」 –

+0

爲什麼要向標記'List'添加一個字節數組?只需添加'line''字符串' – Reimeus

+0

yessssssssss it工作!!! :) –