943,670 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 995
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
May 24th, 2009
0

It Prints nothing from the ArrayList?

Expand Post »
Java Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
caps_lock is offline Offline
43 posts
since Nov 2008
May 24th, 2009
0

Re: It Prints nothing from the ArrayList?

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


Java Syntax (Toggle Plain Text)
  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??
Reputation Points: 10
Solved Threads: 0
Light Poster
caps_lock is offline Offline
43 posts
since Nov 2008
May 24th, 2009
0

Re: It Prints nothing from the ArrayList?

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.
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is online now Online
5,765 posts
since Apr 2008
May 24th, 2009
0

Re: It Prints nothing from the ArrayList?

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());


}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pmalhotra_10 is offline Offline
3 posts
since May 2009
May 24th, 2009
0

Re: It Prints nothing from the ArrayList?

wow nicely explained thanks are you a teacher?

yep next step recurssion
Reputation Points: 10
Solved Threads: 0
Light Poster
caps_lock is offline Offline
43 posts
since Nov 2008
May 25th, 2009
0

Re: It Prints nothing from the ArrayList?

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

Quote ...
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

java Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is online now Online
5,765 posts
since Apr 2008
May 25th, 2009
0

Re: It Prints nothing from the ArrayList?

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:

Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
caps_lock is offline Offline
43 posts
since Nov 2008
May 25th, 2009
0

Re: It Prints nothing from the ArrayList?

Click to Expand / Collapse  Quote originally posted by caps_lock ...
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:

Java Syntax (Toggle Plain Text)
  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?
Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 9
Solved Threads: 3
Newbie Poster
jaka.ramdani is offline Offline
18 posts
since Sep 2008
May 25th, 2009
0

Re: It Prints nothing from the ArrayList?

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!
Reputation Points: 10
Solved Threads: 0
Light Poster
caps_lock is offline Offline
43 posts
since Nov 2008
May 25th, 2009
0

Re: It Prints nothing from the ArrayList?

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

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Urgent Help!
Next Thread in Java Forum Timeline: Switch Case





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC