Results 1 to 10 of 10
  1. #1
    gcclinux is offline Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    20
    Rep Power
    0

    Default Converting a println out in to a method

    I have a class that returns a result to command line using println (list);

    THIS IS THE RESULT WHEN I RUN THE FIRST CLASS

    C:\>java -cp Details.jar packages.ListFiles
    C:\temp\bkdb.txt
    C:\temp\nbu6.5.6\BitmapImage.bmp
    C:\temp\nbu6.5.6\Something.txt
    C:\temp\New Bitmap Image.bmp
    C:\temp\New Text Document.txt

    Java Code:
    public static void main(String[] args) throws FileNotFoundException {
        // USING java.io.File
        File folder = new File("C:/temp");
        // USING java.util.List
          List<File> contents = ListFiles1.getFileListing(folder);
    
    
          // PRINT OUT THE LIST OF FILES
    
    
            for (File list : contents) {
                if (list.isFile() && list.getName().toLowerCase().endsWith(".txt")
                     || list.getName().toLowerCase().endsWith(".bmp")
                     || list.getName().toLowerCase().endsWith(".doc")
            )
             System.out.println(list);
            }
    }
    So I have aquestion then, can I take this code I have with a for loop on specific files and convert that to a method so that the second class can access it?

    Many Thanks

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,923
    Rep Power
    8

    Default

    You sure can. What happened when you tried?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    gcclinux is offline Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    20
    Rep Power
    0

    Default

    I keep getting "cannot return a value from a method whose result type is void" but I don't know how to do otherwise. This bit of code above has taken me weeks to figure out and getting it to work as I am still learning to program java, so everything is very slow to figure it out but that is part of learning and I learn from examples :)

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

  5. #5
    gcclinux is offline Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    20
    Rep Power
    0

    Default

    Hi Darryl.Burke I had found that page via serching google, I unerstand what void doesn't return a value. and that is my problem as for me to capture the output and pass it on the the second class it can not be void, but I can not figure out how to change the first class to give me the same result unless I use void... catch 22

    I know I need to read allot of documentation and I have been doing so but things don't always make sense.

    Weel thanks for the link anyway.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    So what do you want to return?
    How are you calling this?
    Presumably the main() method?
    What are you requirements?

  7. #7
    gcclinux is offline Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    20
    Rep Power
    0

    Default

    Hi Tolls, well I know that I need change it so that instead of pronting the files to the console, it builds a List of the file and returns it so that I can then create a second class and call the first class to get the result (list of file names)

    Does that make sense? Thanks for trying to help.

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Java Code:
        // USING java.io.File
        File folder = new File("C:/temp");
        // USING java.util.List
          List<File> contents = ListFiles1.getFileListing(folder);
    
    
          // PRINT OUT THE LIST OF FILES
    
    
            for (File list : contents) {
                if (list.isFile() && list.getName().toLowerCase().endsWith(".txt")
                     || list.getName().toLowerCase().endsWith(".bmp")
                     || list.getName().toLowerCase().endsWith(".doc")
            )
             System.out.println(list);
            }
    take all of it out of the main method, do something like this:

    Java Code:
    public static void main(String[] args) {
        String output = getFileList().toString();
        System.out.println(output);
    }
    
    public static List<File> getFileList() {
        //your code
        return contents;
    }

    then from your other class you can get the same list:
    firstClass.getFileList()

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    Essentially what ozzyman says, but you'll have to change what happens in your loop, building up a second List that will be what you return, rather than the contents List.

  10. #10
    gcclinux is offline Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    20
    Rep Power
    0

    Thumbs up

    You guys are great, thank you for your help I managed to get it to work just like I wanted, I did change the code and moved the for loop with file extentions to the second class :)

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.List;
    
    public class ShowFiles {
     public static void main(String[] args) throws FileNotFoundException {
    
        List<File> listFiles = ScanFiles.getFileList();
    
        for (File aList : listFiles) {
    
                if (aList.isFile() && aList.getName().toLowerCase().endsWith(".txt")
                     || aList.getName().toLowerCase().endsWith(".bmp")
                     || aList.getName().toLowerCase().endsWith(".doc"))
    
        System.out.println(aList);
         }
      }
    }

Similar Threads

  1. Println VS system.out.println
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 05-20-2010, 08:52 AM
  2. Need help with println
    By jhetfield18 in forum New To Java
    Replies: 8
    Last Post: 09-18-2009, 08:26 AM
  3. println question
    By robocop in forum New To Java
    Replies: 1
    Last Post: 03-11-2009, 06:02 AM
  4. difference between system.out.println() & out.println()
    By wickedrahul9 in forum Advanced Java
    Replies: 5
    Last Post: 10-18-2008, 11:06 PM
  5. Help me with system.out.println
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:30 PM

Posting Permissions

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