Results 1 to 10 of 10
- 04-27-2011, 03:47 PM #1
Member
- Join Date
- Mar 2011
- Location
- London, UK
- Posts
- 20
- Rep Power
- 0
return list - rather than println()
Hello Again,
I am still playing with this Java stuff and trying to learn from examplo and experience.
I created a simple little java class that is returning just what I want, but it is returning it on a console using println() and what I want is to be able to call it so i can do a for loop in a second class with the output of this class.
CLASS:
OUTPUT:Java Code:public class ReadHash { public static void main(String[] args) throws IOException { Properties config = new Properties(); try { // the configuration file name String fileName = "B:/config.txt"; InputStream is = new FileInputStream(fileName); // load the properties file config.load(is); BufferedReader list = new BufferedReader(new FileReader(config.getProperty("file.db"))); for(String line = list.readLine();line != null;line = list.readLine()) { String[] fields = line.split(","); System.out.println(fields[2]); } } catch (FileNotFoundException e) { System.out.println(e); } catch (IOException e) { System.out.println(e); } } }
So what I want is, instead of:Java Code:-201434993 -924925757 -429540048 919857431 1873228496
I want to:Java Code:System.out.println(fields[2]);
So that from another class I can do a for each one of those numbers read a text file and see if it already exist somewhere in that txt file.Java Code:return (fields[2]);
But that is another story, for now I just want to replace println for return;
Can anyone advise? Many Thanks
- 04-27-2011, 04:07 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
The main( ... ) method is a void method so it can't return anything. Besides a method has to return something to a caller that knows how to handle the returned value. To what should the main( ... ) method return anything? You should implement other methods that can be called by the main( ... ) method and return something to that main( ... ) method.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-27-2011, 04:16 PM #3
Member
- Join Date
- Mar 2011
- Location
- London, UK
- Posts
- 20
- Rep Power
- 0
I am going to try it, but I got to admit you have confused me a bit, if you could provide me a little example that would be cool, I already have removed the try {} and going to work work on your recommendation... :-)
- 04-27-2011, 04:17 PM #4
In your program, you have all the logic contained inside the main method, which is of type void(meaning it does not return anything). You could create a method like:
This may not be the most efficient way to do it, but it will work. All of your logic is the same, with the exception of creating a string, and appending to it all of your field[2] values, and then creating your output array by doing a split on it. In thinking about it, I was not sure how you could create just a String array, because when you declare it, you have to know the size of it, and that is a function of the number of lines in the properties file. So without looping through twice, I was not srue how you could accomplish it with just a single String[] and not use the String at the beginning of the method. Someone else knows how, im sure.Java Code:public String[] getFields2(){ String output = ""; Properties config = new Properties(); try { // the configuration file name String fileName = "B:/config.txt"; InputStream is = new FileInputStream(fileName); // load the properties file config.load(is); BufferedReader list = new BufferedReader(new FileReader(config.getProperty("file.db"))); for(String line = list.readLine();line != null;line = list.readLine()) { String[] fields = line.split(","); output = output+" "+fields[2]; } } catch (FileNotFoundException e) { System.out.println(e); } catch (IOException e) { System.out.println(e); } String[] outputArr = output.split(" "); return outputArr; }
You can make the method even more generic by passing in (as parameters) possibly the filename and the delimiter, if needed. So inside of main you could set your String array to be = to getFields2(), and then do whatever manipulation you wanted.Last edited by sehudson; 04-27-2011 at 04:24 PM.
- 04-28-2011, 02:01 PM #5
Member
- Join Date
- Mar 2011
- Location
- London, UK
- Posts
- 20
- Rep Power
- 0
I have been playing with your examplo and I don't see to get it to work, also think that to clarify the B:/config.txt file only points to the real file with all the data:
The real file is a csv containing from a few to hundreds of rows and what I am doing is generating a list from field 2, that equals that list of numbers.
My ultimate gol is:
I have 2 csv files,
File 1 = original list
File 2 = original list + it has additional lines.
Then by looping through File 2 creating a list of all those individual numbers (field[2]), I want a second class looping for each number read each line in File 1 if exist ignore it, if that number does not exist, then create File 3 with a full line in File 2
Basically compare 2 csv files and take out only the lines that exist 1 but not in both.
I hope this make sense as I think I have confused even myself now.
- 04-28-2011, 02:19 PM #6
Member
- Join Date
- Mar 2011
- Location
- London, UK
- Posts
- 20
- Rep Power
- 0
Simplefied version of my initial class:
Java Code:public class ReturnHashNoConf { public static void main(String[] args) throws IOException { File recordsA = new File("C:/Temp/db_file.log"); BufferedReader list = new BufferedReader(new FileReader(recordsA)); for(String line = list.readLine();line != null;line = list.readLine()) { String[] fields = line.split(","); System.out.println(fields[2]); } } }
- 04-28-2011, 03:04 PM #7
Tell me what you get when you run this.Java Code:public class ReturnHashNoConf { String[] out; public static void main(String[] args) throws IOException { out = getArray(); for(int i=0; i<out.length; i++){ System.out.println(out[i]+"\n"); } } public static String[] getArray(){ String results = ""; File recordsA = new File("C:/Temp/db_file.log"); BufferedReader list = new BufferedReader(new FileReader(recordsA)); for(String line = list.readLine();line != null;line = list.readLine()) { String[] fields = line.split(","); results = results+fields[2]+" "; } int len = results.length(); results = results.substring(0,len-1) String[] resArr = results.split(" "); return resArr; } }
- 04-28-2011, 03:22 PM #8
Member
- Join Date
- Mar 2011
- Location
- London, UK
- Posts
- 20
- Rep Power
- 0
Hi sehudson, I can't compile as I am getting an error on this section:
non-static variable out cannot be referenced from a static contextJava Code:out = getArray(); for(int i=0; i<out.length; i++){ System.out.println(out[i]+"\n"); }
Does that make sense?
- 04-28-2011, 03:29 PM #9
so just make out static:
public static String[] out;
- 04-28-2011, 03:41 PM #10
Member
- Join Date
- Mar 2011
- Location
- London, UK
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
System.out.println(040|343);
By satheshshiva in forum New To JavaReplies: 5Last Post: 06-26-2010, 10:26 PM -
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 -
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