Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Java
  String path = "C:\\Users\\User_2\\Pictures";
   
  String files;
  File folder = new File(path);
  
  
  File[] listOfFiles = folder.listFiles(); 
 
  int dot;
  
  for (int i = 0; i < listOfFiles.length; i++) 
  {
   
   if (listOfFiles[i].isFile()){
   files = listOfFiles[i].getName();
   System.out.println(files);
      dot = files.lastIndexOf(".");
   System.out.println(files.substring( dot + 1));
// why this allows equals false......
   System.out.println(files.substring(dot + 1) == "jpg");
  }
  }
Posted
Updated 24-Aug-12 12:24pm
v2
Comments
pasztorpisti 24-Aug-12 18:27pm    
And where is the boolean?
enhzflep 24-Aug-12 18:58pm    
Presumably here?
files.substring(dot + 1) == "jpg"
pasztorpisti 24-Aug-12 19:03pm    
My bad, thanks! Found it! :-)

1 solution

This expression compares the references that "point" to two different string objects. You compare the references ("the pointers") and the the string objects. This gives you false if you compare th references to two distinct string objects even if their value is the same.
Java
files.substring(dot + 1) == "jpg"

Use ths code if you want to compare the contents of the objects instead of the references:
Java
"jpg".equals(files.substring(dot + 1))

I call the method of the "jpg" string object because that is a constant and isn't null for sure and equals() handles well when its parameter is null.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900