Results 1 to 10 of 10
- 03-31-2011, 02:54 PM #1
Member
- Join Date
- Mar 2011
- Location
- London, UK
- Posts
- 20
- Rep Power
- 0
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
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?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); } }
Many Thanks
- 03-31-2011, 02:55 PM #2
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!
- 03-31-2011, 03:11 PM #3
Member
- Join Date
- Mar 2011
- Location
- London, UK
- Posts
- 20
- Rep Power
- 0
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 :)
- 03-31-2011, 03:33 PM #4
- 03-31-2011, 04:02 PM #5
Member
- Join Date
- Mar 2011
- Location
- London, UK
- Posts
- 20
- Rep Power
- 0
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.
- 03-31-2011, 04:18 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,471
- Rep Power
- 16
So what do you want to return?
How are you calling this?
Presumably the main() method?
What are you requirements?
- 03-31-2011, 04:26 PM #7
Member
- Join Date
- Mar 2011
- Location
- London, UK
- Posts
- 20
- Rep Power
- 0
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.
-
take all of it out of the main method, do something like this: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); }
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()
- 03-31-2011, 04:31 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,471
- Rep Power
- 16
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.
- 04-01-2011, 10:02 AM #10
Member
- Join Date
- Mar 2011
- Location
- London, UK
- Posts
- 20
- Rep Power
- 0
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
-
Println VS system.out.println
By ccie007 in forum New To JavaReplies: 2Last Post: 05-20-2010, 08:52 AM -
Need help with println
By jhetfield18 in forum New To JavaReplies: 8Last Post: 09-18-2009, 08:26 AM -
println question
By robocop in forum New To JavaReplies: 1Last Post: 03-11-2009, 06:02 AM -
difference between system.out.println() & out.println()
By wickedrahul9 in forum Advanced JavaReplies: 5Last Post: 10-18-2008, 11:06 PM -
Help me with system.out.println
By baltimore in forum New To JavaReplies: 1Last Post: 07-31-2007, 08:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks