title: Check file extension in Java
body: <p>I have to import data from an Excel file to database and to do this, I would like to check the extension of the chosen file.</p>
  
  <p>This is my code:</p>
  
  <pre><code>String filename = file.getName();
  String extension = filename.substring(filename.lastIndexOf(".") + 1, filename.length());
  
  String excel = "xls";
  if (extension != excel) {
      JOptionPane.showMessageDialog(null, "Choose an excel file!");
  }
  else {
      String filepath = file.getAbsolutePath();
      JOptionPane.showMessageDialog(null, filepath);
      String upload = UploadPoData.initialize(null, filepath);
  
      if (upload == "OK") {
          JOptionPane.showMessageDialog(null, "Upload Successful!");
      }
  }
  </code></pre>
  
  <p>But I always get:</p>
  
  <blockquote>
    <p>Choose an excel file!</p>
  </blockquote>
  
  <p>I can't find what is wrong with my code, could someone please help.</p>