Separating Parsing Method











up vote
0
down vote

favorite
1












I'm creating a program which handles SKU's. I currently have two classes in my program, the SKU class is the main class and a Store class in which an ArrayList is initialised and SKU objects are stored in the array. I currently have a method in my SKU class which takes input from a file, parses the data and stores the data using a String tokenizer in the objects variables and adds the objects to the array in the Store class. The problem I'm facing is that I'm wanting to separate the parsing method in the SKU class so that it simply reads from a line, and then have a separate method which takes a file input for the parser and finally update my Store class so that it initialises the products with the parsed data. Please, can you help me in regards to this?



My parsing method in the SKU class is currently as follows:



public void parser() {
try {
// create a Buffered Reader object instance with a FileReader
BufferedReader br = new BufferedReader(new FileReader("products.txt"));

// read from first line from the text file
String fileRead = br.readLine();
// skip first line from sample file as it contains headings
int lineNumber = 0;

// loop until all lines are read
while (fileRead != null)
{
if(lineNumber == 0) {
lineNumber++;
continue;
}

lineNumber++;

// use string.split to load a string array with the values from each line of
// the file, using a tab as the delimiter
String tokenize = fileRead.split("t");

// assume file is made correctly
// and make temporary variables for the three types of data
String tempProductCode = tokenize[0];
String tempDescription = tokenize[1];
BigDecimal tempPrice = new BigDecimal(tokenize[2]);

// create temporary instance of SKU object
// and load with three data values
SKU tempObj = new SKU();
tempObj.setProductCode(tempProductCode);
tempObj.setDescription(tempDescription);
tempObj.setPrice(tempPrice);

// add to array list
Store.mySkuArrayList.add(tempObj);

// read next line before looping
// if end of file reached
fileRead = br.readLine();
}

// close file stream
br.close();
}

// handle exceptions
catch (FileNotFoundException fnfe)
{
System.out.println("file not found");
}

catch (IOException ioe)
{
ioe.printStackTrace();
}
}


My Store class is as follows:



public class Store {
public static ArrayList<SKU> mySkuArrayList = new ArrayList<SKU>();

public void addSKU(SKU sku) {
mySkuArrayList.add(sku);
}









share|improve this question




















  • 1




    This sounds more like a code review to refactor your code.
    – Nkosi
    Apr 2 at 11:23

















up vote
0
down vote

favorite
1












I'm creating a program which handles SKU's. I currently have two classes in my program, the SKU class is the main class and a Store class in which an ArrayList is initialised and SKU objects are stored in the array. I currently have a method in my SKU class which takes input from a file, parses the data and stores the data using a String tokenizer in the objects variables and adds the objects to the array in the Store class. The problem I'm facing is that I'm wanting to separate the parsing method in the SKU class so that it simply reads from a line, and then have a separate method which takes a file input for the parser and finally update my Store class so that it initialises the products with the parsed data. Please, can you help me in regards to this?



My parsing method in the SKU class is currently as follows:



public void parser() {
try {
// create a Buffered Reader object instance with a FileReader
BufferedReader br = new BufferedReader(new FileReader("products.txt"));

// read from first line from the text file
String fileRead = br.readLine();
// skip first line from sample file as it contains headings
int lineNumber = 0;

// loop until all lines are read
while (fileRead != null)
{
if(lineNumber == 0) {
lineNumber++;
continue;
}

lineNumber++;

// use string.split to load a string array with the values from each line of
// the file, using a tab as the delimiter
String tokenize = fileRead.split("t");

// assume file is made correctly
// and make temporary variables for the three types of data
String tempProductCode = tokenize[0];
String tempDescription = tokenize[1];
BigDecimal tempPrice = new BigDecimal(tokenize[2]);

// create temporary instance of SKU object
// and load with three data values
SKU tempObj = new SKU();
tempObj.setProductCode(tempProductCode);
tempObj.setDescription(tempDescription);
tempObj.setPrice(tempPrice);

// add to array list
Store.mySkuArrayList.add(tempObj);

// read next line before looping
// if end of file reached
fileRead = br.readLine();
}

// close file stream
br.close();
}

// handle exceptions
catch (FileNotFoundException fnfe)
{
System.out.println("file not found");
}

catch (IOException ioe)
{
ioe.printStackTrace();
}
}


My Store class is as follows:



public class Store {
public static ArrayList<SKU> mySkuArrayList = new ArrayList<SKU>();

public void addSKU(SKU sku) {
mySkuArrayList.add(sku);
}









share|improve this question




















  • 1




    This sounds more like a code review to refactor your code.
    – Nkosi
    Apr 2 at 11:23















up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I'm creating a program which handles SKU's. I currently have two classes in my program, the SKU class is the main class and a Store class in which an ArrayList is initialised and SKU objects are stored in the array. I currently have a method in my SKU class which takes input from a file, parses the data and stores the data using a String tokenizer in the objects variables and adds the objects to the array in the Store class. The problem I'm facing is that I'm wanting to separate the parsing method in the SKU class so that it simply reads from a line, and then have a separate method which takes a file input for the parser and finally update my Store class so that it initialises the products with the parsed data. Please, can you help me in regards to this?



My parsing method in the SKU class is currently as follows:



public void parser() {
try {
// create a Buffered Reader object instance with a FileReader
BufferedReader br = new BufferedReader(new FileReader("products.txt"));

// read from first line from the text file
String fileRead = br.readLine();
// skip first line from sample file as it contains headings
int lineNumber = 0;

// loop until all lines are read
while (fileRead != null)
{
if(lineNumber == 0) {
lineNumber++;
continue;
}

lineNumber++;

// use string.split to load a string array with the values from each line of
// the file, using a tab as the delimiter
String tokenize = fileRead.split("t");

// assume file is made correctly
// and make temporary variables for the three types of data
String tempProductCode = tokenize[0];
String tempDescription = tokenize[1];
BigDecimal tempPrice = new BigDecimal(tokenize[2]);

// create temporary instance of SKU object
// and load with three data values
SKU tempObj = new SKU();
tempObj.setProductCode(tempProductCode);
tempObj.setDescription(tempDescription);
tempObj.setPrice(tempPrice);

// add to array list
Store.mySkuArrayList.add(tempObj);

// read next line before looping
// if end of file reached
fileRead = br.readLine();
}

// close file stream
br.close();
}

// handle exceptions
catch (FileNotFoundException fnfe)
{
System.out.println("file not found");
}

catch (IOException ioe)
{
ioe.printStackTrace();
}
}


My Store class is as follows:



public class Store {
public static ArrayList<SKU> mySkuArrayList = new ArrayList<SKU>();

public void addSKU(SKU sku) {
mySkuArrayList.add(sku);
}









share|improve this question















I'm creating a program which handles SKU's. I currently have two classes in my program, the SKU class is the main class and a Store class in which an ArrayList is initialised and SKU objects are stored in the array. I currently have a method in my SKU class which takes input from a file, parses the data and stores the data using a String tokenizer in the objects variables and adds the objects to the array in the Store class. The problem I'm facing is that I'm wanting to separate the parsing method in the SKU class so that it simply reads from a line, and then have a separate method which takes a file input for the parser and finally update my Store class so that it initialises the products with the parsed data. Please, can you help me in regards to this?



My parsing method in the SKU class is currently as follows:



public void parser() {
try {
// create a Buffered Reader object instance with a FileReader
BufferedReader br = new BufferedReader(new FileReader("products.txt"));

// read from first line from the text file
String fileRead = br.readLine();
// skip first line from sample file as it contains headings
int lineNumber = 0;

// loop until all lines are read
while (fileRead != null)
{
if(lineNumber == 0) {
lineNumber++;
continue;
}

lineNumber++;

// use string.split to load a string array with the values from each line of
// the file, using a tab as the delimiter
String tokenize = fileRead.split("t");

// assume file is made correctly
// and make temporary variables for the three types of data
String tempProductCode = tokenize[0];
String tempDescription = tokenize[1];
BigDecimal tempPrice = new BigDecimal(tokenize[2]);

// create temporary instance of SKU object
// and load with three data values
SKU tempObj = new SKU();
tempObj.setProductCode(tempProductCode);
tempObj.setDescription(tempDescription);
tempObj.setPrice(tempPrice);

// add to array list
Store.mySkuArrayList.add(tempObj);

// read next line before looping
// if end of file reached
fileRead = br.readLine();
}

// close file stream
br.close();
}

// handle exceptions
catch (FileNotFoundException fnfe)
{
System.out.println("file not found");
}

catch (IOException ioe)
{
ioe.printStackTrace();
}
}


My Store class is as follows:



public class Store {
public static ArrayList<SKU> mySkuArrayList = new ArrayList<SKU>();

public void addSKU(SKU sku) {
mySkuArrayList.add(sku);
}






java parsing file-io






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 2 at 11:26









Justin Albano

1,53311032




1,53311032










asked Apr 2 at 11:20









Spartan123

529




529








  • 1




    This sounds more like a code review to refactor your code.
    – Nkosi
    Apr 2 at 11:23
















  • 1




    This sounds more like a code review to refactor your code.
    – Nkosi
    Apr 2 at 11:23










1




1




This sounds more like a code review to refactor your code.
– Nkosi
Apr 2 at 11:23






This sounds more like a code review to refactor your code.
– Nkosi
Apr 2 at 11:23














2 Answers
2






active

oldest

votes

















up vote
1
down vote













Split your code to three separate classes. SkuFile class represents text file where sku codes is stored, this class knows how to every sku entry stored and able to parse it. Sku class contains data. Store class contains
list of Sku and accept SkuFile in it's constructor.



class SkuFile {
private String path;

SkuFile(String path) {
this.path = path;
}


List<Sku> readAllSku() {
ArrayList<Sku> result = new ArrayList<>();
try {
List<String> lines = Files.readAllLines(new File(path).toPath());
for(String skuLine : lines) {
result.add(parseFrom(skuLine));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return result;
}

private Sku parseFrom(String data){
String tokenize = data.split("t");
productCode = tokenize[0];
description = tokenize[1];
price = new BigDecimal(tokenize[2]);
return new Sku(productCode, description, price);
}
}

class Sku {
private String code;
private String description;
private BigDecimal price;

Sku(String code, String description, BigDecimal price) {
this.code = code;
this.description = description;
this.price = price;
}

//getters setters methods
}


class Store {
private List<Sku> skus;

Store(SkuFile file) {
skus = file.readAllSku();
}
}


class Test {

public static void main(String args) {
Store store = new Store(new SkuFile("products.txt"));
}
}





share|improve this answer




























    up vote
    0
    down vote













    One way to handle this is by making the parse method return a list of tokenizers(e.g. List tokenizeList) and a second method which takes that list as input and populates the SkuArrayList



    Possible implementation of the parser method



    public List<String> parser() {
    List<String> tokenizeList = new ArrayList<>();
    try {
    ... /*file opening logic*/
    while (fileRead != null)
    {
    .../*line counting logic*/
    String tokenize = fileRead.split("t");
    tokenizeList.add(tokenize);

    fileRead = br.readLine();
    }

    // close file stream
    br.close();
    }// handle exceptions
    catch (FileNotFoundException fnfe)
    {
    System.out.println("file not found");
    }
    catch (IOException ioe)
    {
    ioe.printStackTrace();
    }
    return tokenizeList;
    }


    Possible implementation of the populate store method



    public void populateStore(List<String> tokenizeList) {
    for(String tokenize: tokenizeList) {
    String tempProductCode = tokenize[0];
    String tempDescription = tokenize[1];
    BigDecimal tempPrice = new BigDecimal(tokenize[2]);

    SKU tempObj = new SKU();
    tempObj.setProductCode(tempProductCode);
    tempObj.setDescription(tempDescription);
    tempObj.setPrice(tempPrice);

    // add to array list
    Store.mySkuArrayList.add(tempObj);
    }
    }


    And the main method from where you call these two methods



    public void foo() {
    populateStore(parser());
    }





    share|improve this answer























    • Thanks, did you mean to pass the 'parser' method to the parameter for the populateStore method in the call under the main method?
      – Spartan123
      Apr 2 at 19:41










    • Maybe main was not the best name for that method( I renamed it to foo). And yes, I wanted to pass the result from the parser method to the populateStore method. Another way you could do that is to store the result of the parser in a variable and pass that variable to the other method.
      – Alexandru Stana
      Apr 2 at 19:57











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49610310%2fseparating-parsing-method%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Split your code to three separate classes. SkuFile class represents text file where sku codes is stored, this class knows how to every sku entry stored and able to parse it. Sku class contains data. Store class contains
    list of Sku and accept SkuFile in it's constructor.



    class SkuFile {
    private String path;

    SkuFile(String path) {
    this.path = path;
    }


    List<Sku> readAllSku() {
    ArrayList<Sku> result = new ArrayList<>();
    try {
    List<String> lines = Files.readAllLines(new File(path).toPath());
    for(String skuLine : lines) {
    result.add(parseFrom(skuLine));
    }
    } catch (IOException e) {
    throw new RuntimeException(e);
    }
    return result;
    }

    private Sku parseFrom(String data){
    String tokenize = data.split("t");
    productCode = tokenize[0];
    description = tokenize[1];
    price = new BigDecimal(tokenize[2]);
    return new Sku(productCode, description, price);
    }
    }

    class Sku {
    private String code;
    private String description;
    private BigDecimal price;

    Sku(String code, String description, BigDecimal price) {
    this.code = code;
    this.description = description;
    this.price = price;
    }

    //getters setters methods
    }


    class Store {
    private List<Sku> skus;

    Store(SkuFile file) {
    skus = file.readAllSku();
    }
    }


    class Test {

    public static void main(String args) {
    Store store = new Store(new SkuFile("products.txt"));
    }
    }





    share|improve this answer

























      up vote
      1
      down vote













      Split your code to three separate classes. SkuFile class represents text file where sku codes is stored, this class knows how to every sku entry stored and able to parse it. Sku class contains data. Store class contains
      list of Sku and accept SkuFile in it's constructor.



      class SkuFile {
      private String path;

      SkuFile(String path) {
      this.path = path;
      }


      List<Sku> readAllSku() {
      ArrayList<Sku> result = new ArrayList<>();
      try {
      List<String> lines = Files.readAllLines(new File(path).toPath());
      for(String skuLine : lines) {
      result.add(parseFrom(skuLine));
      }
      } catch (IOException e) {
      throw new RuntimeException(e);
      }
      return result;
      }

      private Sku parseFrom(String data){
      String tokenize = data.split("t");
      productCode = tokenize[0];
      description = tokenize[1];
      price = new BigDecimal(tokenize[2]);
      return new Sku(productCode, description, price);
      }
      }

      class Sku {
      private String code;
      private String description;
      private BigDecimal price;

      Sku(String code, String description, BigDecimal price) {
      this.code = code;
      this.description = description;
      this.price = price;
      }

      //getters setters methods
      }


      class Store {
      private List<Sku> skus;

      Store(SkuFile file) {
      skus = file.readAllSku();
      }
      }


      class Test {

      public static void main(String args) {
      Store store = new Store(new SkuFile("products.txt"));
      }
      }





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        Split your code to three separate classes. SkuFile class represents text file where sku codes is stored, this class knows how to every sku entry stored and able to parse it. Sku class contains data. Store class contains
        list of Sku and accept SkuFile in it's constructor.



        class SkuFile {
        private String path;

        SkuFile(String path) {
        this.path = path;
        }


        List<Sku> readAllSku() {
        ArrayList<Sku> result = new ArrayList<>();
        try {
        List<String> lines = Files.readAllLines(new File(path).toPath());
        for(String skuLine : lines) {
        result.add(parseFrom(skuLine));
        }
        } catch (IOException e) {
        throw new RuntimeException(e);
        }
        return result;
        }

        private Sku parseFrom(String data){
        String tokenize = data.split("t");
        productCode = tokenize[0];
        description = tokenize[1];
        price = new BigDecimal(tokenize[2]);
        return new Sku(productCode, description, price);
        }
        }

        class Sku {
        private String code;
        private String description;
        private BigDecimal price;

        Sku(String code, String description, BigDecimal price) {
        this.code = code;
        this.description = description;
        this.price = price;
        }

        //getters setters methods
        }


        class Store {
        private List<Sku> skus;

        Store(SkuFile file) {
        skus = file.readAllSku();
        }
        }


        class Test {

        public static void main(String args) {
        Store store = new Store(new SkuFile("products.txt"));
        }
        }





        share|improve this answer












        Split your code to three separate classes. SkuFile class represents text file where sku codes is stored, this class knows how to every sku entry stored and able to parse it. Sku class contains data. Store class contains
        list of Sku and accept SkuFile in it's constructor.



        class SkuFile {
        private String path;

        SkuFile(String path) {
        this.path = path;
        }


        List<Sku> readAllSku() {
        ArrayList<Sku> result = new ArrayList<>();
        try {
        List<String> lines = Files.readAllLines(new File(path).toPath());
        for(String skuLine : lines) {
        result.add(parseFrom(skuLine));
        }
        } catch (IOException e) {
        throw new RuntimeException(e);
        }
        return result;
        }

        private Sku parseFrom(String data){
        String tokenize = data.split("t");
        productCode = tokenize[0];
        description = tokenize[1];
        price = new BigDecimal(tokenize[2]);
        return new Sku(productCode, description, price);
        }
        }

        class Sku {
        private String code;
        private String description;
        private BigDecimal price;

        Sku(String code, String description, BigDecimal price) {
        this.code = code;
        this.description = description;
        this.price = price;
        }

        //getters setters methods
        }


        class Store {
        private List<Sku> skus;

        Store(SkuFile file) {
        skus = file.readAllSku();
        }
        }


        class Test {

        public static void main(String args) {
        Store store = new Store(new SkuFile("products.txt"));
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 2 at 12:14









        fxrbfg

        1,2951515




        1,2951515
























            up vote
            0
            down vote













            One way to handle this is by making the parse method return a list of tokenizers(e.g. List tokenizeList) and a second method which takes that list as input and populates the SkuArrayList



            Possible implementation of the parser method



            public List<String> parser() {
            List<String> tokenizeList = new ArrayList<>();
            try {
            ... /*file opening logic*/
            while (fileRead != null)
            {
            .../*line counting logic*/
            String tokenize = fileRead.split("t");
            tokenizeList.add(tokenize);

            fileRead = br.readLine();
            }

            // close file stream
            br.close();
            }// handle exceptions
            catch (FileNotFoundException fnfe)
            {
            System.out.println("file not found");
            }
            catch (IOException ioe)
            {
            ioe.printStackTrace();
            }
            return tokenizeList;
            }


            Possible implementation of the populate store method



            public void populateStore(List<String> tokenizeList) {
            for(String tokenize: tokenizeList) {
            String tempProductCode = tokenize[0];
            String tempDescription = tokenize[1];
            BigDecimal tempPrice = new BigDecimal(tokenize[2]);

            SKU tempObj = new SKU();
            tempObj.setProductCode(tempProductCode);
            tempObj.setDescription(tempDescription);
            tempObj.setPrice(tempPrice);

            // add to array list
            Store.mySkuArrayList.add(tempObj);
            }
            }


            And the main method from where you call these two methods



            public void foo() {
            populateStore(parser());
            }





            share|improve this answer























            • Thanks, did you mean to pass the 'parser' method to the parameter for the populateStore method in the call under the main method?
              – Spartan123
              Apr 2 at 19:41










            • Maybe main was not the best name for that method( I renamed it to foo). And yes, I wanted to pass the result from the parser method to the populateStore method. Another way you could do that is to store the result of the parser in a variable and pass that variable to the other method.
              – Alexandru Stana
              Apr 2 at 19:57















            up vote
            0
            down vote













            One way to handle this is by making the parse method return a list of tokenizers(e.g. List tokenizeList) and a second method which takes that list as input and populates the SkuArrayList



            Possible implementation of the parser method



            public List<String> parser() {
            List<String> tokenizeList = new ArrayList<>();
            try {
            ... /*file opening logic*/
            while (fileRead != null)
            {
            .../*line counting logic*/
            String tokenize = fileRead.split("t");
            tokenizeList.add(tokenize);

            fileRead = br.readLine();
            }

            // close file stream
            br.close();
            }// handle exceptions
            catch (FileNotFoundException fnfe)
            {
            System.out.println("file not found");
            }
            catch (IOException ioe)
            {
            ioe.printStackTrace();
            }
            return tokenizeList;
            }


            Possible implementation of the populate store method



            public void populateStore(List<String> tokenizeList) {
            for(String tokenize: tokenizeList) {
            String tempProductCode = tokenize[0];
            String tempDescription = tokenize[1];
            BigDecimal tempPrice = new BigDecimal(tokenize[2]);

            SKU tempObj = new SKU();
            tempObj.setProductCode(tempProductCode);
            tempObj.setDescription(tempDescription);
            tempObj.setPrice(tempPrice);

            // add to array list
            Store.mySkuArrayList.add(tempObj);
            }
            }


            And the main method from where you call these two methods



            public void foo() {
            populateStore(parser());
            }





            share|improve this answer























            • Thanks, did you mean to pass the 'parser' method to the parameter for the populateStore method in the call under the main method?
              – Spartan123
              Apr 2 at 19:41










            • Maybe main was not the best name for that method( I renamed it to foo). And yes, I wanted to pass the result from the parser method to the populateStore method. Another way you could do that is to store the result of the parser in a variable and pass that variable to the other method.
              – Alexandru Stana
              Apr 2 at 19:57













            up vote
            0
            down vote










            up vote
            0
            down vote









            One way to handle this is by making the parse method return a list of tokenizers(e.g. List tokenizeList) and a second method which takes that list as input and populates the SkuArrayList



            Possible implementation of the parser method



            public List<String> parser() {
            List<String> tokenizeList = new ArrayList<>();
            try {
            ... /*file opening logic*/
            while (fileRead != null)
            {
            .../*line counting logic*/
            String tokenize = fileRead.split("t");
            tokenizeList.add(tokenize);

            fileRead = br.readLine();
            }

            // close file stream
            br.close();
            }// handle exceptions
            catch (FileNotFoundException fnfe)
            {
            System.out.println("file not found");
            }
            catch (IOException ioe)
            {
            ioe.printStackTrace();
            }
            return tokenizeList;
            }


            Possible implementation of the populate store method



            public void populateStore(List<String> tokenizeList) {
            for(String tokenize: tokenizeList) {
            String tempProductCode = tokenize[0];
            String tempDescription = tokenize[1];
            BigDecimal tempPrice = new BigDecimal(tokenize[2]);

            SKU tempObj = new SKU();
            tempObj.setProductCode(tempProductCode);
            tempObj.setDescription(tempDescription);
            tempObj.setPrice(tempPrice);

            // add to array list
            Store.mySkuArrayList.add(tempObj);
            }
            }


            And the main method from where you call these two methods



            public void foo() {
            populateStore(parser());
            }





            share|improve this answer














            One way to handle this is by making the parse method return a list of tokenizers(e.g. List tokenizeList) and a second method which takes that list as input and populates the SkuArrayList



            Possible implementation of the parser method



            public List<String> parser() {
            List<String> tokenizeList = new ArrayList<>();
            try {
            ... /*file opening logic*/
            while (fileRead != null)
            {
            .../*line counting logic*/
            String tokenize = fileRead.split("t");
            tokenizeList.add(tokenize);

            fileRead = br.readLine();
            }

            // close file stream
            br.close();
            }// handle exceptions
            catch (FileNotFoundException fnfe)
            {
            System.out.println("file not found");
            }
            catch (IOException ioe)
            {
            ioe.printStackTrace();
            }
            return tokenizeList;
            }


            Possible implementation of the populate store method



            public void populateStore(List<String> tokenizeList) {
            for(String tokenize: tokenizeList) {
            String tempProductCode = tokenize[0];
            String tempDescription = tokenize[1];
            BigDecimal tempPrice = new BigDecimal(tokenize[2]);

            SKU tempObj = new SKU();
            tempObj.setProductCode(tempProductCode);
            tempObj.setDescription(tempDescription);
            tempObj.setPrice(tempPrice);

            // add to array list
            Store.mySkuArrayList.add(tempObj);
            }
            }


            And the main method from where you call these two methods



            public void foo() {
            populateStore(parser());
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 11 at 20:36

























            answered Apr 2 at 11:41









            Alexandru Stana

            645




            645












            • Thanks, did you mean to pass the 'parser' method to the parameter for the populateStore method in the call under the main method?
              – Spartan123
              Apr 2 at 19:41










            • Maybe main was not the best name for that method( I renamed it to foo). And yes, I wanted to pass the result from the parser method to the populateStore method. Another way you could do that is to store the result of the parser in a variable and pass that variable to the other method.
              – Alexandru Stana
              Apr 2 at 19:57


















            • Thanks, did you mean to pass the 'parser' method to the parameter for the populateStore method in the call under the main method?
              – Spartan123
              Apr 2 at 19:41










            • Maybe main was not the best name for that method( I renamed it to foo). And yes, I wanted to pass the result from the parser method to the populateStore method. Another way you could do that is to store the result of the parser in a variable and pass that variable to the other method.
              – Alexandru Stana
              Apr 2 at 19:57
















            Thanks, did you mean to pass the 'parser' method to the parameter for the populateStore method in the call under the main method?
            – Spartan123
            Apr 2 at 19:41




            Thanks, did you mean to pass the 'parser' method to the parameter for the populateStore method in the call under the main method?
            – Spartan123
            Apr 2 at 19:41












            Maybe main was not the best name for that method( I renamed it to foo). And yes, I wanted to pass the result from the parser method to the populateStore method. Another way you could do that is to store the result of the parser in a variable and pass that variable to the other method.
            – Alexandru Stana
            Apr 2 at 19:57




            Maybe main was not the best name for that method( I renamed it to foo). And yes, I wanted to pass the result from the parser method to the populateStore method. Another way you could do that is to store the result of the parser in a variable and pass that variable to the other method.
            – Alexandru Stana
            Apr 2 at 19:57


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49610310%2fseparating-parsing-method%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python