0

I have done returning list from one method and trying to receive the list data from another method to write in excel file but it is printing only last row. please help me where i am missing the part. thanks in advance. 1.Method one readingFile is reading the text file 2. Second method is to writing file from the list data received from method one

My code with two methods are as follows

   package one;  
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class AutoPasswordReset 
{
    public String input_path = ("C:\\Users\\RAVI\\Desktop\\Skills\\inputs");
    public String output_path = ("C:\\Users\\RAVI\\Desktop\\Skills\\outputs");
    BufferedReader br;
    SimpleDateFormat sdf = new SimpleDateFormat("hh:MM.ss");
    SimpleDateFormat parsingSdf = new SimpleDateFormat("hh:MM.ss a");
    Object[] received = new Object[3];
    static ArrayList<Object[]> list= new ArrayList<Object[]>();

    public  ArrayList<Object[]>  readingFile() throws IOException{
        try{
            File fi = new File(input_path);
            File[] fileCount = fi.listFiles();
            for(int i = 0;i<fileCount.length;i++){
                File file = fileCount[i];
                if(file.isFile()){
                    System.out.println("Total file count : "+fileCount.length);
                    String fileName = file.getName();
                    System.out.println("File name : "+fileName);
                    String data;
                    br = new BufferedReader(new FileReader(input_path+"\\"+fileName));
                    while((data = br.readLine())!=null){
                        if(data.contains(">")){
                            String dat = data.substring(data.indexOf(" ")+1,data.indexOf("-")-1);
                            //System.out.println(dat);
                            Date date = sdf.parse(dat.substring(dat.indexOf(" "),dat.lastIndexOf(".")));
                            //System.out.println(date);
                            String timeFormat = parsingSdf.format(date);
                            //System.out.println(timeFormat);
                            received[0] = dat.substring(dat.indexOf("0"),dat.indexOf(" ")+1)+timeFormat;
                            //System.out.println(received[0]);
                            received[1] = data.substring(data.indexOf("<")+1,data.indexOf(",")-1);
                            //System.out.println(received[1]);
                            received[2] = data.substring(data.indexOf("Target"),data.lastIndexOf("."));
                            //System.out.println(received[2]);
                            list.add(received);

                        }
                    }

                }
            }

        }catch(FileNotFoundException | ParseException fe){
            System.out.println(fe.getMessage());
        }
        return list;
    }
    public  void writingFile(ArrayList<Object[]> list){
        try{
             /*for(int i=0;i<list.size();i++){
                    Object[] obj = list.get(i);
                    System.out.println(obj[0]);
             }*/
            AutoPasswordReset apr = new AutoPasswordReset();
        ArrayList<Object[]> listTwo = apr.readingFile();
        for(Object[] data : listTwo){
            System.out.println(data[0]+" "+data[1]+" "+data[1]);
            //Unable to get all datas it is printing only last row from file
        }
        }catch(Exception e){
            e.printStackTrace();

        }

    }
    public static void main(String[] args) throws FileNotFoundException {
        AutoPasswordReset ar = new AutoPasswordReset();
        ar.writingFile(list);
        // Unable to pass as argument after changed to non static variable 

        try{
        }catch (Exception fnt){
            System.out.println(fnt.getMessage());
            fnt.printStackTrace();
        }

    }
}
  • 2
    It's happening because you are always adding the same reference to the same `static Object[] received`. Rather than declaring a single one at the class level, you should declare a new one for each line being read. – Patrick Parker Aug 16 '18 at 15:16
  • Hi Radiodef i made non static variable but still i am not able to pass list as argument from main let me update my code again in above screen –  Aug 16 '18 at 15:17
  • Hi Patrick parker i have updated my code again and posting still not working thanks for your time and helping me out –  Aug 16 '18 at 15:21
  • please read more carefully my comment and the answers in the linked duplicate. It explains exactly what you have done wrong. – Patrick Parker Aug 16 '18 at 15:23
  • Patrick Parker bro i have read those and did modified variables to non static now in main method i am not able to pass list it is showing me to change it to static again ar.writingFile(list); // Unable to pass as argument after changed to non static variable –  Aug 16 '18 at 15:25
  • i have added after entire loops in try block then it is running only once instead of for 21 times for 21 rows with last row patrick –  Aug 16 '18 at 15:46
  • It is not working still now –  Aug 16 '18 at 16:07
  • Where i have to modify exactly tried all possible inside and outside loops to add objects into list list.add(received); –  Aug 16 '18 at 16:08
  • It's better not to use arrays in conjunction with `List`s. You could instead, for example, use `List>`. – MC Emperor Aug 16 '18 at 16:25
  • Can you please tell me more specific patrick. in the end of try block i added list.add(obj); then also it is not working –  Aug 16 '18 at 16:28
  • Hi MC Emperor , i am not sure where you are pointing out i am facing problem in below two declaration 1. static ArrayList list= new ArrayList(); 2. Object[] received = new Object[3]; –  Aug 16 '18 at 16:31
  • why list adding last row for 20 times can anyone help me where i am doing wrong –  Aug 16 '18 at 16:40
  • just removing the word "static" is not enough. you have not understood my comment or the linked answers. try reading them again or ask in [chat](https://chat.stackoverflow.com/rooms/139/java). – Patrick Parker Aug 16 '18 at 16:52
  • I do not have enough reputation to chat you can tell me more specific i could understood to avoid adding to same reference what i have to do patrick plz help me thanks –  Aug 16 '18 at 17:01
  • Is anyone help me , fine let me add all my codes into main method to fix... thanks guys –  Aug 16 '18 at 17:29
  • what part of this comment is confusing? "declare a new Object[] received for each line being read". You have to ***move*** the ***declaration*** to your inner loop of the read function. – Patrick Parker Aug 16 '18 at 18:30
  • Hi Patrick bro, i just declared inside while loop where it is starting to read and it is working fine now. while((data = br.readLine())!=null) { Object[] received = new Object[3]; why bro what does the difference it take, i just declared an object array with size of 3 right and adding each column after substring from a file to add why it is not worked before bro....Thanks much , and when u find free time plz update why it is not worked before. thanks again Patrick bro –  Aug 16 '18 at 22:50
  • so what i can understand is each time object array need to be created to add each line i mean row from a file, is that right bro Patrick... –  Aug 17 '18 at 00:46
  • I think you should study the explanation in the linked duplicate. In particular, what is a Java reference variable and how can two variables point to the same object. – Patrick Parker Aug 17 '18 at 06:11

0 Answers0