Results 1 to 6 of 6
- 05-25-2009, 06:48 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 12
- Rep Power
- 0
ArrayLists do not print to the terminal window, why? long code inside
I think the title summarizes nicely
Java Code: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) { } } }
whats gone wrong here? im trying to compare the results in tow different arrayLists, but nothing prints to the terminal window?
feel free to copy paste and compile
any help welcome
- 05-25-2009, 07:45 PM #2
What do you mean nothing prints out? Something must print out
This is either going to print out a number or an exception will be thrown (and probably displayed). Maybe you could show us what is printed out so far. That way we don't have to compile + run + debug it for you.Java Code:System.out.println (filesOnComputer.length);
Make sure you give us all of the output.
Mr. Beans
- 05-25-2009, 08:07 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 12
- Rep Power
- 0
oh ok no worries.
The code compiles fine.
and this is a typical result that it prints out to the terminal window
$IXWE7L7.bmp
heres another result in the terminal window
$Recycle.Bin
here we are---autoexec.bat
lol i got confused with a previous version of my class, how embarassing, still doesnt do what i want it
i.e. print all files from the two arraylists that are equal in size but unequal in hash code value
- 05-25-2009, 08:14 PM #4
Looking over your code a little more I'm confused on the statement
I believeJava Code:if (arrayList.get(index).length() == arrayListAgain.get(index).length() && arrayList.get(index).hashCode() != arrayListAgain.get(index).hashCode())
So your if statement is essentiallyJava Code:arrayList.get(index) == arrayListAgain.get(index); // both return reference to the same object
Thus nothing is being printed out from the if statement.Java Code:if( true && false)
- 05-25-2009, 08:44 PM #5
Member
- Join Date
- Jan 2009
- Posts
- 12
- Rep Power
- 0
hmmm perhaps, but my basic understanding is is that:
ArrayList 1:
File X, 5 bytes, Hashcode 555
File Y, 6 bytes, Hashcode 766
ArrayList 2:
File XX, 5 bytes, Hashcode 889
File YY, 7 bytes, Hashcode 222
Wanted Terminal Window Result:
File XX, 5 bytes, Hashcode 889
File X, 5 bytes, Hashcode 555
Thats what i take that if statement to be doing
- 05-25-2009, 09:03 PM #6
Thats what you want it to print out. However, it's not printing anything out because the if statement is evaluated to be false and thus the code block that follows it is not executed.
Your doing something like
Java Code:if( obj1.length() == obj1.length() && obj1.hashCode() != obj1.hashCode() ) { // this is never going to be executed }
Maybe you should simply add all the valid files to one ArrayList then check each file in the ArrayList to the other files.
If you didn't have the if statement to deviate from the print statement you would get something like
File X, 5 bytes, Hashcode 889
File X, 5 bytes, Hashcode 889
Mr. BeansLast edited by Mr.Beans; 05-25-2009 at 09:05 PM.
Similar Threads
-
Generate a random code 4 letters long
By bl00dr3d in forum New To JavaReplies: 9Last Post: 04-06-2009, 05:32 AM -
println doesn't print from inside for loop, et.al.
By rdtindsm in forum New To JavaReplies: 5Last Post: 03-27-2009, 01:19 PM -
Running .java-files won't work/compile does!(Code inside)
By wyldstyle in forum New To JavaReplies: 6Last Post: 02-06-2009, 08:05 PM -
How to Print out adiamond pattern I try this code but
By masaka in forum New To JavaReplies: 11Last Post: 04-16-2008, 01:52 PM -
main window & print dialog
By roo7 in forum Advanced JavaReplies: 3Last Post: 01-02-2008, 06:50 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks