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 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:
    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); }
        }
    }
    OUTPUT:

    Java Code:
    -201434993
    -924925757
    -429540048
    919857431
    1873228496
    So what I want is, instead of:

    Java Code:
    System.out.println(fields[2]);
    I want to:

    Java Code:
    return (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.

    But that is another story, for now I just want to replace println for return;

    Can anyone advise? Many Thanks

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    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... :-)

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    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:

    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;
    
    
    }
    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.

    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.

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

    Default

    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.

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

    Default

    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]);
            }
        }
    
    }

  7. #7
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    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;
    
    }
    
    
    }
    Tell me what you get when you run this.

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

    Default

    Hi sehudson, I can't compile as I am getting an error on this section:

    Java Code:
      out = getArray();
        for(int i=0; i<out.length; i++){
        System.out.println(out[i]+"\n");
    }
    non-static variable out cannot be referenced from a static context

    Does that make sense?

  9. #9
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    so just make out static:

    public static String[] out;

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

    Default

    Thanks for all your help, I changed it a little and managed to get out exacly what I wanted.

    Java Code:
    public class ReturnHashNoConf {
    
        public static void main(String[] args) throws IOException {
            String[] out = getArray();
    
            for(int i=0; i<out.length; i++){
            System.out.println(out[i]);
            }
    }

Similar Threads

  1. System.out.println(040|343);
    By satheshshiva in forum New To Java
    Replies: 5
    Last Post: 06-26-2010, 10:26 PM
  2. Println VS system.out.println
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 05-20-2010, 08:52 AM
  3. Need help with println
    By jhetfield18 in forum New To Java
    Replies: 8
    Last Post: 09-18-2009, 08:26 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
  •