954,164 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

It Prints nothing from the ArrayList?

import java.io.*;
import java.util.*;
 
public class MainTwo {
public static void main(String[] args) {
        for(File file : File.listRoots()){ 
            search(file);
        }
    }

    public static void search(File f) 
    {  
        ArrayList<File> arrayList = new ArrayList<File>();
            f.listFiles();
            arrayList.add(f);
            
            System.out.println (f.getName());
        
        
    }
}


Why does it print nothing?

caps_lock
Light Poster
43 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

i just tried a differrent approach, this code also prints out nothing

import java.io.*;
import java.util.*;
 
public class MainTwo 
{
    public static void main(String[] args) 
    {
        File[] filesOnComputer = File.listRoots(); 
        {
            ArrayList<File> listOfFiles = new ArrayList<File>();
            for(int index = 0; index < filesOnComputer.length; index++)
            {
                listOfFiles.add(filesOnComputer[index]);    
                System.out.println (filesOnComputer[index].getName());
            }
        }
    }
}


why does it print out nothing??

caps_lock
Light Poster
43 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Do you mean prints nothing, or just "c:"? Because your code just seems to print the results of the listRoots. You do call listFiles, but you do nothing with it.

JamesCherrill
Posting Genius
Moderator
6,337 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,070
 

i see two issues:
1) First u r calling listRoots, tht must print C:\
2) Secondly, u r not trying to get the iterate through the list of files that u got...

Let see ur code:
File[] filesOnComputer = File.listRoots();

this part shall give u the root of the directory structure (in most cases it would be C:, in case it is partioned it might also give D:)

then u will have to chk whether this is a directory, and if it is then u will have to call ur search function with little modification as:

public static void search(File f)
{
ArrayList arrayList = new ArrayList();
File[] files= f.listFiles(); // get all files under the direcrory
for(int index = 0; index < files.length; index++)
{
System.out.println ("here we are---"+files[index].getName());

arrayList.add(files[index].getName());
}
System.out.println (f.getName());


}

u might need to also call this recursively to see if the next selection is a file or a directory.

Try this :

public static void main(String[] args)
{
File[] filesOnComputer = File.listRoots();
{
ArrayList listOfFiles = new ArrayList();
System.out.println (filesOnComputer.length);

for(int index = 0; index < filesOnComputer.length; index++)
{
listOfFiles.add((File)filesOnComputer[index]);
if(filesOnComputer[index].isDirectory()){
System.out.println ("here we are-"+filesOnComputer[index].getPath());
search((File)listOfFiles.get(index));
}
else
System.out.println ("This is not the directory-"+filesOnComputer[index].getPath());
}
}
}
public static void search(File f)
{
ArrayList arrayList = new ArrayList();
File[] files= f.listFiles();
for(int index = 0; index < files.length; index++)
{
System.out.println ("here we are---"+files[index].getName());

arrayList.add(files[index].getName());
}
System.out.println (f.getName());


}

pmalhotra_10
Newbie Poster
3 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

wow nicely explained thanks are you a teacher?

yep next step recurssion

caps_lock
Light Poster
43 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Rather than the pre-1.5 old-style for loops and un-typed lists, replace

ArrayList arrayList = new ArrayList(); File[] files= f.listFiles(); // get all files under the direcrory for(int index = 0; index < files.length; index++) { System.out.println ("here we are---"+files[index].getName());

arrayList.add(files[index].getName()); }

with

ArrayList arrayList<File> = new ArrayList<File>();
for(File ff : f.listFiles()) {
  System.out.println ("here we are---"+ff.getName());
  arrayList.add(ff.getName());
}
JamesCherrill
Posting Genius
Moderator
6,337 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,070
 

i got the recursion working, couldnt quite implent the new for loop, moved on to the next step, ei. trying to compare files in two ArrayLists, and the first hiccup is as follows:

ArrayList arrayList = new ArrayList();
            ArrayList arrayListAgain = new ArrayList();
            
            File[] files= f.listFiles();    
            for(int index = 0; index < files.length; index++)
            
            if (files[index].isFile())
            {
                 
                System.out.println ("here we are---"+files[index].getName());
               
                arrayList.add(files[index]);
                arrayListAgain.add(files[index]);
                
                if (arrayList(files[index]).length() == arrayListAgain(files[index]).length() && arrayList(files[index]).hashCode() != arrayListAgain(files[index]).hashCode())
                {
                    //do something;
                }
            }

Hmm my compiler friend tells me cannot find symbol - method arrayList(Java.io.File)??

The java class library for ArrayList was not helpful to me.

caps_lock
Light Poster
43 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

i got the recursion working, couldnt quite implent the new for loop, moved on to the next step, ei. trying to compare files in two ArrayLists, and the first hiccup is as follows:

ArrayList arrayList = new ArrayList();
            ArrayList arrayListAgain = new ArrayList();
            
            File[] files= f.listFiles();    
            for(int index = 0; index < files.length; index++)
            
            if (files[index].isFile())
            {
                 
                System.out.println ("here we are---"+files[index].getName());
               
                arrayList.add(files[index]);
                arrayListAgain.add(files[index]);
                
                if (arrayList(files[index]).length() == arrayListAgain(files[index]).length() && arrayList(files[index]).hashCode() != arrayListAgain(files[index]).hashCode())
                {
                    //do something;
                }
            }

Hmm my compiler friend tells me cannot find symbol - method arrayList(Java.io.File)??

The java class library for ArrayList was not helpful to me.

maybe you mean something like this?

ArrayList<File> arrayList = new ArrayList<File>();
    ArrayList<File> arrayListAgain = new ArrayList<File>();
    
    File[] files= f.listFiles();    
    for(int index = 0; index < files.length; index++)
    
    if (files[index].isFile())
    {
         
        System.out.println ("here we are---" + files[index].getName());
       
        arrayList.add(files[index]);
        arrayListAgain.add(files[index]);
        
        if (arrayList.get(index).length() == arrayListAgain.get(index).length() && 
          arrayList.get(index).hashCode() != arrayListAgain.get(index).hashCode())
        {
            //do something;
        }
    }
jaka.ramdani
Newbie Poster
18 posts since Sep 2008
Reputation Points: 9
Solved Threads: 3
 

thanks jaka.ramdani


I tried making the modifcation you siggested, but the compiler error with that was:

cannot find symbol - method length

and then i tried using (files[index]) in the if statement, and then it said cannot find symbol - method get


so frustrating!

caps_lock
Light Poster
43 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

oh my fault i didnt make all the changes it does compile, rep for you!

caps_lock
Light Poster
43 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 
oh my fault i didnt make all the changes it does compile, rep for you!

glad it helps .. thanks for the rep.

jaka.ramdani
Newbie Poster
18 posts since Sep 2008
Reputation Points: 9
Solved Threads: 3
 

would some one be kind enough to copy and paste and compile this code, I get no results to the terminal, my criteria should bring a few results

import java.io.*;
import java.util.*;
 
public class MainFourA 
{
    public static void main(String[] args)    
    {
        File[] filesOnComputer = File.listRoots(); 
   
            ArrayList listOfFiles = new ArrayList();
            System.out.println (filesOnComputer.length);
            for(int index = 0; index < filesOnComputer.length; index++)
            {
                listOfFiles.add((File)filesOnComputer[index]); 
                if(filesOnComputer[index].isDirectory())
                {
                    System.out.println ("here we are-"+filesOnComputer[index].getPath());
                    search((File)listOfFiles.get(index));
                }
                else
                System.out.println ("This is not the directory-"+filesOnComputer[index].getPath());
            }
        }
    
    public static void search(File f) 
    {
        try
        { 
            ArrayList<File> arrayList = new ArrayList<File>();
            ArrayList<File> arrayListAgain = new ArrayList<File>();
    
            File[] files= f.listFiles();    
            for(int index = 0; index < files.length; index++)
            
            if (files[index].isDirectory())
            {
                search(files[index]);
            }

            else if (files[index].isFile())
            {
         
                System.out.println ("here we are---" + files[index].getName());
       
                arrayList.add(files[index]);
                arrayListAgain.add(files[index]);
        
                if (arrayList.get(index).length() == arrayListAgain.get(index).length() && 
                arrayList.get(index).hashCode() != arrayListAgain.get(index).hashCode())
                {
                    
                        System.out.println (arrayList.get(index).getName() + ", " + arrayList.get(index).length() + " bytes."); 

                }
    } 
            

            System.out.println (f.getName());
        }
            catch (Exception e)
            {
            }
        }
    }


it compiles, if you comment out the big if statement, then lots of results come to the terminal window

I hope know one is getting annoyed by me!

caps_lock
Light Poster
43 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

it does print now:

this is some sample output

$R3QSQ3M
here we are---$R49HV83.txt
$Recycle.Bin
here we are---autoexec.bat

confusing...

caps_lock
Light Poster
43 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You