代码应该读取文件并检查它。如果数据类型或关键字或操作。代码编译没有错误,但没有出现 jTextArea 检查的结果。可能是我忘记了一些东西,但 jTextArea 什么也没读。
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) {
}
}