`Getter`没有在`Setter`块之外检索真值

时间:2018-03-12 16:00:32

标签: java csv foreach setter getter

我正在尝试从2个不同的CSV文件中导入2个不同的行作为列表。

从一个CSV文件中获取另一个CSV文件中的shareNumberListmultiplierList

然后尝试使用foreach循环在另一个列表shareNumberList中设置multiplierListinputCalculationList的值。

尝试在setter块(#1&#2)中获取值时,然后获取正确的值。但是在尝试从另一个代码块中获取值时(#3)然后得到零。尽管我在一个全局声明的列表中添加了setter值。

inputCalculationList.add(inputCalculation)

那我为什么不从另一个街区获取价值?

我的完整代码:

public class CSVFileRWFileHandler {

    public static List<InputCalculation> inputCalculationList = new ArrayList<InputCalculation>();
    public static List<ShareNumber> shareNumberList = new ArrayList<ShareNumber>();
    public static List<Multiplier> multiplierList = new ArrayList<Multiplier>();
    public static InputCalculation inputCalculation = new InputCalculation();

public static void readCsvFile(String fileName, 
    List<InterMediateCalculation> imCalculationList) {

    ReadFilesForCalcImpl rfc = new ReadFilesForCalcImpl();
    //getting *shareNumberList*
    rfc.readShareNumberSheet(Constant.readShareNunmerFileLocation, shareNumberList); 
    //getting *multiplierList*
    rfc.readMultiplierSheet(Constant.readMultiplierFileLocation, multiplierList);
 }
 public static void writeCsvFile(String fileName) {     
    FileWriter fileWriter = null;               
    try {
        fileWriter = new FileWriter(fileName);
        fileWriter.append(Constant.FILE_HEADER);            
        fileWriter.append(Constant.NEW_LINE_SEPARATOR);            

       //block #1:
      for (Multiplier multiplier : multiplierList) {
           // setter for inputCalculation
           inputCalculation.setMultiplierAW(multiplier.getMultiplierAW());
           // getter for inputCalculation
           System.out.println(inputCalculation.getMultiplierAW());//This line prints proper value
           inputCalculationList.add(inputCalculation);              
       }
       //block #2:
       for (ShareNumber shareNumber : shareNumberList) {    
            // setter for inputCalculation                  
          inputCalculation.setShareNumberAW(shareNumber.getShareNumberAW());    
          // getter for inputCalculation
          System.out.println(inputCalculation.getShareNumberAW());//This line prints proper value
          inputCalculationList.add(inputCalculation);               
       }
      //block #3:       
      for (InputCalculation inputCalculation : inputCalculationList) {
          System.out.println(inputCalculation.getShareNumberAW());//This line don't print proper value. Getting '0' 
          System.out.println(inputCalculation.getMultiplierAW());   //This line don't print proper value. Getting '0'

          fileWriter.append(inputCalculation.getShareNumberAW()); //getter not fetchging original value.
          fileWriter.append(Constant.COMMA_DELIMITER);
          fileWriter.append(inputCalculation.getMultiplierAW());  //getter not fetchging original value.
          fileWriter.append(Constant.NEW_LINE_SEPARATOR);

       }catch (Exception e) {
        System.out.println("Error in CsvFileWriter !!!");
        e.printStackTrace();
       } 
  }

readMultiplierSheet方法:

 public void readMultiplierSheet(String readfileName, List<Multiplier> multiplierList) {
    BufferedReader fileReader = null;       
    try {
        String line = "";
        fileReader = new BufferedReader(new FileReader(readfileName));
        fileReader.readLine();
        while ((line = fileReader.readLine()) != null) {
            String[] tokens = line.split(Constant.COMMA_DELIMITER);   
            if (tokens.length > 0) {
                Multiplier multiplier = new Multiplier(tokens[Constant.Cell_INDEX_OF_AW], tokens[Constant.Cell_INDEX_OF_AX]);
                multiplierList.add(multiplier);
            }
        }
    }
    catch (Exception e) {
        System.out.println("Error in CsvFileReader !!!");
        e.printStackTrace();
    } finally {
        try {
            fileReader.close();
        } catch (IOException e) {
            System.out.println("Error while closing fileReader !!!");
            e.printStackTrace();
        }
    }   
}

readShareNumberSheet方法类似于readMultiplierSheet方法。

提前致谢。

3 个答案:

答案 0 :(得分:1)

尝试使用单个循环并重新创建其中的每个项目,与此类似

for(int index = 0; index < multiplierList.length; index++){
  public static List<InputCalculation> inputCalculationList = new ArrayList<InputCalculation>();

  inputCalculation.setMultiplierAW( multiplierList.get(index).getMultiplierAW());

  inputCalculation.setShareNumberAW( shareNumberList.get(index).getShareNumberAW());  
  inputCalculationList.add(inputCalculation); 
}

答案 1 :(得分:0)

//block #1:
>       for (Multiplier multiplier : multiplierList) {
>            // setter for inputCalculation
>            inputCalculation.setMultiplierAW(multiplier.getMultiplierAW());
>            // getter for inputCalculation
>            System.out.println(inputCalculation.getMultiplierAW());//This line
> prints proper value
>            inputCalculationList.add(inputCalculation);              
>        }

我认为你可以在每次更新foreach循环inputCalculation对象后保留现有的实现。但这不是一个好方法。

答案 2 :(得分:0)

实际上,我已经通过构造函数在shareNumberListmultiplierList中设置了值:

ShareNumber shareNumber = new ShareNumber(tokens[Constant.Cell_INDEX_OF_AW]); 
shareNumberList.add(shareNumber);

Multiplier multiplier = new Multiplier(tokens[Constant.Cell_INDEX_OF_AW], tokens[Constant.Cell_INDEX_OF_AX]);
multiplierList.add(multiplier);

如果我在这里尝试使用构造函数,我的代码就像this_

InputCalculation inputCalculation = new InputCalculation(hareNumber.getShareNumberAW(), multiplier.getMultiplierAW());

block #1我得到multiplier.getMultiplierAW()block #2我得到shareNumber.getShareNumberAW()

所以我无法从一个块同时获得2个值。所以不能在这里使用构造函数进行实例化。

这就是为什么我必须采取以下方式_

for(int index = 0; index < shareNumberList.size(); index++){

    inputCalculation.setMultiplierAW( multiplierList.get(index).getMultiplierAW());
    inputCalculation.setShareNumberAW( shareNumberList.get(index).getShareNumberAW());  

    // others calculation
    // Write in file as output.
    ...............................................

}