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.
This is my 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!"); } }
But I always get:
Choose an excel file!
I can't find what is wrong with my code, could someone please help.
following
extension != excel
should be
!excel.equals(extension)
or
!excel.equalsIgnoreCase(extension)
See also
equals()
versus ==
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With