Stephopolis
Stephopolis

Reputation: 1795

Return value from method java

I have a program in java that I wrote to return a table of values. Later on as the functions of this program grew I found that I would like to access a variable within the method that isn't returned but I am not sure the best way to go about it. I know that you cannot return more than one value but how would I go about accessing this variable without a major overhaul? here is a simplified version of my code:

public class Reader {
    public String[][] fluidigmReader(String cllmp) throws IOException {
        //read in a file
        while ((inpt = br.readLine()) != null) {
            if (!inpt.equals("Calls")) {
                continue;
            }
            break;
        }
        br.readLine();
        inpt = br.readLine();
        //set up parse parse parameters and parse
        prse = inpt.split(dlmcma, -1);
        while ((inpt = br.readLine()) != null) {
            buffed.add(inpt);
        }
        int lncnt = 0;
        String tbl[][] = new String[buffed.size()][rssnps.size()];
        for (int s = 0; s < buffed.size(); s++) {
            prse = buffed.get(s).split(dlmcma);
            //turns out I want this smpls ArrayList elsewhere
            smpls.add(prse[1]);
//making the table to search through
            for (int m = 0; m < prse.length; m++) {
                tbl[lncnt][m] = prse[m];
            }
            lncnt++;
        }
        //but I return just the tbl here
        return tbl;
    }

Can anyone recommend a way to use smpls in another class without returning it? Is this perhaps when you use a get/set sort of setup? Sorry if this seems like an obvious question, I am still new to the world of modular programming

Upvotes: 0

Views: 2718

Answers (4)

Munish Katoch
Munish Katoch

Reputation: 517

You can use class(Inside Reader class) variable for this. But make sure that it's read/write is synchronized

Upvotes: 0

Has QUIT--Anony-Mousse
Has QUIT--Anony-Mousse

Reputation: 77454

If you had used a dedicated class for the return value (such as the TableWrapper mentioned in another answer), then you could add additional fields there.

That is the good thing about classes - they can be extended. But you cannot extend String[][] in Java.

Upvotes: 1

corsiKa
corsiKa

Reputation: 82559

Right now you have this tbl variable. Wrap it in a class and add the list to the class.

class TableWrapper {
    // default accessing for illustrative purposes - 
    // setters and getters are a good idea
    String[][] table;
    List<String> samples;

    TableWrapper(String[][] table, List<String> samples) {
        this.table = table;
        this.samples = samples;
    }
}

Then refactor your method to return the wrapper object.

public TableWrapper fluidigmReader(String cllmp) throws IOException {
    // your code here
    String tbl[][] = new String[buffed.size()][rssnps.size()];
    TableWrapper tw = new TableWrapper(tbl,smpls);
    // more of your code
    return tw;
}  

Then later in your code where you were going

String[][] tbl = fluidigmReader(cllmp);

You instead go

TableWrapper tw = fluidigmReader(cllmp);
String[][] tbl = tw.table;
List<String> smpls = tw.samples;

Upvotes: 3

Peter Lawrey
Peter Lawrey

Reputation: 533432

You can set a field, instead of a local variable, which you can retrieve later with a getter. You want to avoid it unless it is needed, but in this case it is.

Upvotes: 0

Related Questions