-1

I am facing a weird Java behavior in following code (it lists folder with 1 sub folder, 2 .mp4 files, and 1 .mp3 file). I am trying to asign different icons to video and other files:

    private void fill(File f)
{
    File[]dirs = f.listFiles();
     this.setTitle("Current Dir: "+f.getName());
     List<Item> dir = new ArrayList<Item>();
     List<Item> fls = new ArrayList<Item>();
     try{
         for(File ff: dirs)
         { 
            Date lastModDate = new Date(ff.lastModified());
            DateFormat formater = DateFormat.getDateTimeInstance();
            String date_modify = formater.format(lastModDate);
            if(ff.isDirectory()){


                File[] fbuf = ff.listFiles();
                int buf = 0;
                if(fbuf != null){ 
                    buf = fbuf.length;
                } 
                else buf = 0; 
                String num_item = String.valueOf(buf);
                if(buf == 0) num_item = num_item + " item";
                else num_item = num_item + " items";

                //String formated = lastModDate.toString();
                dir.add(new Item(ff.getName(),num_item,date_modify,ff.getAbsolutePath(),"directory_icon")); 
            }
            else
            {
                String ext = "";

                ext=getFileExtension(ff);
                Log.d("Zox","'"+ext+"'");
                if(ext!="mp4"){
                    Log.d("Zox","'"+ext+"'");
                    Log.d("Zox","FU");
                }
                else {
                    fls.add(new Item(ff.getName(), ff.length() + " Byte", date_modify, ff.getAbsolutePath(), "file_icon"));
                }
            }
         }

I get the following output:

08-09 18:54:08.219 11057-11057/com.piandro D/Zox: 'mp4'

08-09 18:54:08.219 11057-11057/com.piandro D/Zox: 'mp4'

08-09 18:54:08.219 11057-11057/com.piandro D/Zox: FU

08-09 18:54:08.219 11057-11057/com.piandro D/Zox: 'mp4'

08-09 18:54:08.219 11057-11057/com.piandro D/Zox: 'mp4'

08-09 18:54:08.219 11057-11057/com.piandro D/Zox: FU

08-09 18:54:08.229 11057-11057/com.piandro D/Zox: 'mp3'

08-09 18:54:08.229 11057-11057/com.piandro D/Zox: 'mp3'

08-09 18:54:08.229 11057-11057/com.piandro D/Zox: FU

The key code part is:

            String ext = "";

            ext=getFileExtension(ff);
            Log.d("Zox","'"+ext+"'");
            if(ext!="mp4"){
                Log.d("Zox","'"+ext+"'");
                Log.d("Zox","FU");
            }
            else {
                fls.add(new Item(ff.getName(), ff.length() + " Byte", date_modify, ff.getAbsolutePath(), "file_icon"));
            }

and condition and action are nonsense, just for debugging purpose. Why Java doesn't recognize String match?

Gruiberg
  • 66
  • 8

2 Answers2

1

You are using != operator to check the inequality of String. You should rather use

if(!ext.equals("mp4")){
                Log.d("Zox","'"+ext+"'");
                Log.d("Zox","FU");
            }
Md Johirul Islam
  • 5,042
  • 4
  • 23
  • 56
0
  • should never use == operator for string match
  • use .equals or .equalIgnoreCase always
vk1
  • 166
  • 6
  • 15