-3

代码应该读取文件并检查它。如果数据类型或关键字或操作。代码编译没有错误,但没有出现 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) {
    }     

    }                                          
4

1 回答 1

0

除了String#equals String已明确标记的比较问题外,主要问题是在尝试调用时BufferedReader br抛出的读取循环内关闭。将语句移动到块中IOExceptionBufferedReader#readLineclosefinally

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

此外,向令牌添加字节数组List将导致JTextArea. 代替

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

tokens.add(line);

Java 中的集合使用泛型来避免强制转换。将它们应用于 的所有实例List,例如

List<String> myList = new ArrayList<>();
于 2013-05-16T14:06:47.407 回答