It Prints nothing from the ArrayList?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 37
Reputation: caps_lock is an unknown quantity at this point 
Solved Threads: 0
caps_lock caps_lock is offline Offline
Light Poster

It Prints nothing from the ArrayList?

 
0
  #1
May 24th, 2009
  1.  
  2.  
  3.  
  4. import java.io.*;
  5. import java.util.*;
  6.  
  7. public class MainTwo {
  8. public static void main(String[] args) {
  9. for(File file : File.listRoots()){
  10. search(file);
  11. }
  12. }
  13.  
  14. public static void search(File f)
  15. {
  16. ArrayList<File> arrayList = new ArrayList<File>();
  17. f.listFiles();
  18. arrayList.add(f);
  19.  
  20. System.out.println (f.getName());
  21.  
  22.  
  23. }
  24. }

Why does it print nothing?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 37
Reputation: caps_lock is an unknown quantity at this point 
Solved Threads: 0
caps_lock caps_lock is offline Offline
Light Poster

Re: It Prints nothing from the ArrayList?

 
0
  #2
May 24th, 2009
i just tried a differrent approach, this code also prints out nothing


  1.  
  2.  
  3.  
  4.  
  5. import java.io.*;
  6. import java.util.*;
  7.  
  8. public class MainTwo
  9. {
  10. public static void main(String[] args)
  11. {
  12. File[] filesOnComputer = File.listRoots();
  13. {
  14. ArrayList<File> listOfFiles = new ArrayList<File>();
  15. for(int index = 0; index < filesOnComputer.length; index++)
  16. {
  17. listOfFiles.add(filesOnComputer[index]);
  18. System.out.println (filesOnComputer[index].getName());
  19. }
  20. }
  21. }
  22. }

why does it print out nothing??
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: It Prints nothing from the ArrayList?

 
0
  #3
May 24th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 3
Reputation: pmalhotra_10 is an unknown quantity at this point 
Solved Threads: 0
pmalhotra_10 pmalhotra_10 is offline Offline
Newbie Poster

Re: It Prints nothing from the ArrayList?

 
0
  #4
May 24th, 2009
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());


}
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 37
Reputation: caps_lock is an unknown quantity at this point 
Solved Threads: 0
caps_lock caps_lock is offline Offline
Light Poster

Re: It Prints nothing from the ArrayList?

 
0
  #5
May 24th, 2009
wow nicely explained thanks are you a teacher?

yep next step recurssion
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: It Prints nothing from the ArrayList?

 
0
  #6
May 25th, 2009
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

  1. ArrayList arrayList<File> = new ArrayList<File>();
  2. for(File ff : f.listFiles()) {
  3. System.out.println ("here we are---"+ff.getName());
  4. arrayList.add(ff.getName());
  5. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 37
Reputation: caps_lock is an unknown quantity at this point 
Solved Threads: 0
caps_lock caps_lock is offline Offline
Light Poster

Re: It Prints nothing from the ArrayList?

 
0
  #7
May 25th, 2009
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:

  1.  
  2.  
  3. ArrayList arrayList = new ArrayList();
  4. ArrayList arrayListAgain = new ArrayList();
  5.  
  6. File[] files= f.listFiles();
  7. for(int index = 0; index < files.length; index++)
  8.  
  9. if (files[index].isFile())
  10. {
  11.  
  12. System.out.println ("here we are---"+files[index].getName());
  13.  
  14. arrayList.add(files[index]);
  15. arrayListAgain.add(files[index]);
  16.  
  17. if (arrayList(files[index]).length() == arrayListAgain(files[index]).length() && arrayList(files[index]).hashCode() != arrayListAgain(files[index]).hashCode())
  18. {
  19. //do something;
  20. }
  21. }


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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 13
Reputation: jaka.ramdani is an unknown quantity at this point 
Solved Threads: 2
jaka.ramdani jaka.ramdani is offline Offline
Newbie Poster

Re: It Prints nothing from the ArrayList?

 
0
  #8
May 25th, 2009
Originally Posted by caps_lock View Post
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:

  1.  
  2.  
  3. ArrayList arrayList = new ArrayList();
  4. ArrayList arrayListAgain = new ArrayList();
  5.  
  6. File[] files= f.listFiles();
  7. for(int index = 0; index < files.length; index++)
  8.  
  9. if (files[index].isFile())
  10. {
  11.  
  12. System.out.println ("here we are---"+files[index].getName());
  13.  
  14. arrayList.add(files[index]);
  15. arrayListAgain.add(files[index]);
  16.  
  17. if (arrayList(files[index]).length() == arrayListAgain(files[index]).length() && arrayList(files[index]).hashCode() != arrayListAgain(files[index]).hashCode())
  18. {
  19. //do something;
  20. }
  21. }


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?
  1. ArrayList<File> arrayList = new ArrayList<File>();
  2. ArrayList<File> arrayListAgain = new ArrayList<File>();
  3.  
  4. File[] files= f.listFiles();
  5. for(int index = 0; index < files.length; index++)
  6.  
  7. if (files[index].isFile())
  8. {
  9.  
  10. System.out.println ("here we are---" + files[index].getName());
  11.  
  12. arrayList.add(files[index]);
  13. arrayListAgain.add(files[index]);
  14.  
  15. if (arrayList.get(index).length() == arrayListAgain.get(index).length() &&
  16. arrayList.get(index).hashCode() != arrayListAgain.get(index).hashCode())
  17. {
  18. //do something;
  19. }
  20. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 37
Reputation: caps_lock is an unknown quantity at this point 
Solved Threads: 0
caps_lock caps_lock is offline Offline
Light Poster

Re: It Prints nothing from the ArrayList?

 
0
  #9
May 25th, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 37
Reputation: caps_lock is an unknown quantity at this point 
Solved Threads: 0
caps_lock caps_lock is offline Offline
Light Poster

Re: It Prints nothing from the ArrayList?

 
0
  #10
May 25th, 2009
oh my fault i didnt make all the changes it does compile, rep for you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC