Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check file extension in Java

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.

like image 405
Joe88 Avatar asked Jun 07 '12 08:06

Joe88


Video Answer


1 Answers

following

extension != excel 

should be

!excel.equals(extension) 

or

!excel.equalsIgnoreCase(extension) 

See also

  • String equals() versus ==
like image 196
jmj Avatar answered Sep 29 '22 15:09

jmj