-3

The code should Read the file and Check it .If Datatype or Keywords or Operation . The code compile with no erros but the jTextArea don't appear The result of check . May be I forgot something but jTextArea don't read anything .

  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;

}

Here the button that when I press on should Read the File and check it.

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) {
    }     

    }                                          

1 Answers1

0

Apart from the String#equals String comparison issue which has been well flagged, the main issue is that BufferedReader br is being closed within the read loop which throws an IOException when attempting to call BufferedReader#readLine. Move the close statement to a finally block

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

Also, adding a byte array to the token List will result in an unreadable work in the JTextArea. Replace

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

with

tokens.add(line);

Collections in Java use generics to avoid casting. Apply them to all instances of List, e.g.

List<String> myList = new ArrayList<>();
Reimeus
  • 158,255
  • 15
  • 216
  • 276