29

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.

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
Joe88
  • 441
  • 1
  • 5
  • 15
  • What value does the `extension` variable get? Great question on string comparisons: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – maksimov Jun 07 '12 at 08:40
  • 3
    As a rule of thumb, use `equalsIgnoreCase()` whenever you are going to check for equality between strings (assuming that you do want to ignore case). – posdef Jun 07 '12 at 08:44
  • 1
    Eeek! This code is going to throw a `StringIndexOutOfBoundsException` when it encounters a filename without any dot/period. – arkon Sep 11 '13 at 05:12
  • stupid code to compare strings using != but anyway the file extension checking code helps – shaunak1111 Dec 09 '13 at 06:25

8 Answers8

28

following

extension != excel

should be

!excel.equals(extension)

or

!excel.equalsIgnoreCase(extension)

See also

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
  • 7
    +1 It should actually be `!excel.equalsCaseIgnore(extension)` ;) – Peter Lawrey Jun 07 '12 at 08:42
  • @peter yeah added it in answer – jmj Jun 07 '12 at 08:43
  • 2
    aha. nice trap Peter, carefully placed. it's actually `equalsIgnoreCase()` not `equalsCaseIgnore`. @JigarJoshi no, it's not 'or equalsIgnoreCase', it really _should_ be `equalsIgnoreCase` and not `equals`. – maksimov Jun 07 '12 at 08:46
7

== tests referentially equality. For value equality test, use .equals. Use String#equalsIgnoreCase if you want the case to be disregarded during the equality test.

Another thing: Don't reinvent the wheel, unless it's badly broken. In your case, you should be using Apache Commons' FilenameUtils#isExtension to check the extension.

missingfaktor
  • 90,905
  • 62
  • 285
  • 365
  • He might need to cut down on the dependencies for one reason or another, what he/she wants to do is such a simple thing that I actually wouldn't advice to add a dependency just for that. – posdef Jun 07 '12 at 09:35
  • @posdef, right. At least he should factor it out in a separate method. – missingfaktor Jun 07 '12 at 09:36
  • @posdef, by the way, if you look at the source of `FilenameUtils#isExtension`, you'll notice that the task isn't as simple as it looks. – missingfaktor Jun 07 '12 at 09:37
  • admittedly even the simplest tasks get complicated once you set out to find generalized solutions that cover all possible options. Doesn't mean that the OP needs that much of an elaborate solution. :) – posdef Jun 07 '12 at 09:41
  • @posdef Unless he's looking to make it bulletproof. In which case he will definitely need an elaborate solution. – arkon Sep 11 '13 at 05:12
6
if (extension != excel){
   JOptionPane.showMessageDialog(null, "Choose an excel file!");

}

should be used as

if (!extension.equals(excel)){
   JOptionPane.showMessageDialog(null, "Choose an excel file!");

}

And

 if (upload == "OK") {
 JOptionPane.showMessageDialog(null,"Upload Successful!");
}

as

 if ("OK".equals(upload)) {
 JOptionPane.showMessageDialog(null,"Upload Successful!");
}
amicngh
  • 7,831
  • 3
  • 35
  • 54
  • -1 for bad code. You really want to execute `equals` on the constant, not on the variable, because your variable can be `null`. So `"OK".equalsIgnoreCase(upload)` is always better than other way around. – maksimov Jun 07 '12 at 08:49
  • thanks, but your update didn't improve anything in relation to what I said above. – maksimov Jun 07 '12 at 09:00
3

use

excel.equals(extension)

or

excel.equalsIgnoreCase(extension)
Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37
3

You can use Apache Commons Api to check the file extension

String filename = file.getName();
if(!FilenameUtils.isExtension(filename,"xls")){
  JOptionPane.showMessageDialog(null, "Choose an excel file!");
}

http://commons.apache.org/io/api-release/index.html?org/apache/commons/io/package-summary.html

Rajesh Pitty
  • 2,823
  • 2
  • 18
  • 28
2

In my programm i did that

if(file_name.endsWith(".xls") || file_name.endsWith(".xlsx"))
   // something works with file
1

use equals() method instead of != symbols in your case. I mean write

if (!(extension.equals(excel))){..........}
Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
  • Thanks man. I'm a bit new to Java, so could you please give me an explanation? I used e.getActionCommand() == "ApproveSelection" to check if the Open button is clicked in a FileChooser and it's working. What is the difference? – Joe88 Jun 07 '12 at 08:46
  • @Joe88 in short == will check the reference but equals() checks the content. You can get many articles in this concept. So google it to know more. – Chandra Sekhar Jun 07 '12 at 08:49
  • @Joe88 generally speaking, for JVM `"ApproveSelection"` is a constant, hence ActionCommand that's been constucted with the same `"ApproveSelection"` will internally point to the same memory. – maksimov Jun 07 '12 at 08:57
0

How about

if (filename.endsWith("xls"){
    <blah blah blah>
}

?

kevlaria
  • 33
  • 5