Results 1 to 6 of 6
  1. #1
    caps_lock is offline Member
    Join Date
    Jan 2009
    Posts
    12
    Rep Power
    0

    Default 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

  2. #2
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    What do you mean nothing prints out? Something must print out
    Java Code:
    System.out.println (filesOnComputer.length);
    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.

    Make sure you give us all of the output.

    Mr. Beans

  3. #3
    caps_lock is offline Member
    Join Date
    Jan 2009
    Posts
    12
    Rep Power
    0

    Default

    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

  4. #4
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Looking over your code a little more I'm confused on the statement

    Java Code:
    if (arrayList.get(index).length() == arrayListAgain.get(index).length() && 
                    arrayList.get(index).hashCode() != arrayListAgain.get(index).hashCode())
    I believe
    Java Code:
    arrayList.get(index) == arrayListAgain.get(index); // both return reference to the same object
    So your if statement is essentially
    Java Code:
    if( true && false)
    Thus nothing is being printed out from the if statement.

  5. #5
    caps_lock is offline Member
    Join Date
    Jan 2009
    Posts
    12
    Rep Power
    0

    Default

    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

  6. #6
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    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. Beans
    Last edited by Mr.Beans; 05-25-2009 at 10:05 PM.

Similar Threads

  1. Generate a random code 4 letters long
    By bl00dr3d in forum New To Java
    Replies: 9
    Last Post: 04-06-2009, 06:32 AM
  2. println doesn't print from inside for loop, et.al.
    By rdtindsm in forum New To Java
    Replies: 5
    Last Post: 03-27-2009, 02:19 PM
  3. Replies: 6
    Last Post: 02-06-2009, 09:05 PM
  4. Replies: 11
    Last Post: 04-16-2008, 02:52 PM
  5. main window & print dialog
    By roo7 in forum Advanced Java
    Replies: 3
    Last Post: 01-02-2008, 07:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •