代碼應該讀取文件並檢查它。如果數據類型或關鍵字或操作。代碼編譯沒有錯誤,但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) {
}
}
'if(x == myList.get(i))'這不是我們如何比較Java中的字符串。這個問題今天被問了兩次:_( – Maroun
但是當我使用時,我得到錯誤 –
仍然我有同樣的問題不要讀任何東西:( –