-4

Here is my problem:

  1. I have a Java String returned from my Web service in the below form:

    21,6.417,0.3055714,27,0.7778,0.04761905

  2. Now I have splitted this string into separate Strings by using split function by comma delimiter.

    dataList = Arrays.asList(data.split(","));

  3. I need to loop through this list and assign these Values into separate Strings like below:

    int playsCount = Integer.parseInt(dataList.get(0));
    float sumTimeViewed = Float.valueOf(dataList.get(1));
    float avgTimeViewed = Float.valueOf(dataList.get(2));
    int loadsCount = Integer.parseInt(dataList.get(3));
    float loadPlayRatio = Float.valueOf(dataList.get(4));
    float avgViewDropOff = Float.valueOf(dataList.get(5));
    
  4. Now While getting the values and assigning to the Individual int and floats, getting ArrayIndexOutofBoundsException. This is because some times the list is returning the size as 4 than 6. Here is the code:

    reportDataList = getReportData(entry.id);
        //System.out.println("reportDataList.size()"+reportDataList.size());
        if(reportDataList.size() >= 1) {
            for(int i=0;i<reportDataList.size();i++) { 
                if(!reportDataList.get(0).equals("")) {
                    playsCount = Integer.parseInt(reportDataList.get(0));
                    }
                if(!reportDataList.get(1).equals("")) {
                    sumTimeViewed = Float.valueOf(reportDataList.get(1));
                    }
                if(!reportDataList.get(2).equals("")) {
                    avgTimeViewed = Float.valueOf(reportDataList.get(2));
                    }
                if(!reportDataList.get(3).equals("")) {
                    loadsCount = Integer.parseInt(reportDataList.get(3));
                    }
                if(!reportDataList.get(4).equals("")) {
                    loadPlayRatio =Float.valueOf(reportDataList.get(4));
                    }
                if(!reportDataList.get(5).equals("")) {
                    avgViewDropOff = Float.valueOf(reportDataList.get(5));
            }
            }
        }
    

And here is the getReportData method:

private List<String> getReportData(String id) throws KalturaApiException {
    List<String> headerList = null;
    List<String> dataList = new ArrayList<String>();
    ReportService reportService = client.getReportService();
    ReportType reportType = ReportType.TOP_CONTENT;
    ReportInputFilter reportInputFilter = new ReportInputFilter();
    reportInputFilter.fromDate = 1390156200;
    reportInputFilter.toDate = 1453660200;
    ReportTotal reportTotal = reportService.getTotal(reportType, reportInputFilter, id);
    String data = reportTotal.data;
    if(data != null) {
        dataList = Arrays.asList(data.split(","));
    }
    if(dataList.size() >= 1) {
        System.out.println("dataList.size() ------->"+dataList.size());
    }
    return dataList;
    }

How to resolve this problem for any List size acceptable?

Thanks in Advance Raji

Raji
  • 23
  • 1
  • 6
  • 4
    "some times the list is returning the size as 4 than 6"; well, there is your explanation. What more do you need to know? – Raedwald Feb 02 '16 at 16:24
  • OK so then check the size and handle accordingly? – Andrew_CS Feb 02 '16 at 16:25
  • Or show the input string that produced 4 elements only. I bet there are only 3 commas – Gavriel Feb 02 '16 at 16:27
  • @ Andrew - I am doing checking the size and handling accordingly. But not sure this is the correct way to do this ... if(reportDataList.size() == 4) { //Handling all date which is 4 } else { // Handling all data which is not 4 } – Raji Feb 02 '16 at 16:29
  • 1
    No one here is going to know what your program needs to do if the list size is 4 instead of 6. That is up to you or your user or whatever specifies what your program is supposed to do. – khelwood Feb 02 '16 at 16:31

2 Answers2

0

I think your problem is due to the unpredictable length of the returned string from the internet.

If you string can have i.e. 4 numbers separated with ',', you'll get 4 strings, with indexes from 0 to 3.

But if you have 6 numbers separated by comma, you'll get 6 strings with index from 0 to 5.

Due to this, I think you'll have to detect the size of the list (using dataList.size() ) and then handle different part of code if, for example, size=4 or size=6.

Also, if your input string from the internet will always have "fixed" different possible sizes (i.e. siez=4 or size=6 and no other sizes) you'll can overcome the problem using a switch(dataList.size()) , but if your length is unpredictable, so you can have sizes always different, I'll suggest you to revise your code in a semantically way that can led you to write better code, because I think your code is much "fixed" for a particulare case. If this is the case, so just continue with your coding. But if it is not, i'll suggest to think of more adaptive solutions.

panc_fab
  • 522
  • 1
  • 7
  • 24
0

This is how I fixed as of now:

if(reportDataList.size() == 4) {
            //System.out.println("Size is 4");
            for(int i=0;i<reportDataList.size();i++) { 
                if(!reportDataList.get(0).equals("")) {
                    playsCount = Integer.parseInt(reportDataList.get(0));
                    //System.out.println("playsCount :"+playsCount);
                    accountVO.setMediaPlays(playsCount);
                }
                if(!reportDataList.get(1).equals("")) {
                    sumTimeViewed = Float.valueOf(reportDataList.get(1));
                    //System.out.println("sumTimeViewed :"+sumTimeViewed);
                    accountVO.setSumTimeViewed(sumTimeViewed);
                }
                if(!reportDataList.get(2).equals("")) {
                    avgTimeViewed = Float.valueOf(reportDataList.get(2));
                    //System.out.println("avgTimeViewed :"+avgTimeViewed);
                    accountVO.setAvgViewTime(avgTimeViewed);
                }
                if(!reportDataList.get(3).equals("")) {
                    loadsCount = Integer.parseInt(reportDataList.get(3));
                    //System.out.println("loadsCount :"+loadsCount);

                }
            }
        }
        else {
            //System.out.println("Size is not 4");
            for(int i=0;i<reportDataList.size();i++) { 
                if(!reportDataList.get(0).equals("")) {
                    playsCount = Integer.parseInt(reportDataList.get(0));
                    accountVO.setMediaPlays(playsCount);
                    //System.out.println("playsCount :"+playsCount);
                }
                if(!reportDataList.get(1).equals("")) {
                    sumTimeViewed = Float.valueOf(reportDataList.get(1));
                    //System.out.println("sumTimeViewed :"+sumTimeViewed);
                    accountVO.setSumTimeViewed(sumTimeViewed);
                }
                if(!reportDataList.get(2).equals("")) {
                    avgTimeViewed = Float.valueOf(reportDataList.get(2));
                    //System.out.println("avgTimeViewed :"+avgTimeViewed);
                    accountVO.setSumTimeViewed(sumTimeViewed);
                }
                if(!reportDataList.get(3).equals("")) {
                    loadsCount = Integer.parseInt(reportDataList.get(3));
                    //System.out.println("loadsCount :"+loadsCount);
                }
                if(!reportDataList.get(4).equals("")) {
                    loadPlayRatio =Float.valueOf(reportDataList.get(4));
                    //System.out.println("loadPlayRatio :"+loadPlayRatio);
                    accountVO.setPlayToImpressionRatio(loadPlayRatio);
                }
                if(!reportDataList.get(5).equals("")) {
                    avgViewDropOff = Float.valueOf(reportDataList.get(5));
                    //System.out.println("avgViewDropOff :"+avgViewDropOff);
                    accountVO.setAvgViewDropOff(avgViewDropOff);
                }
            }

        }
Raji
  • 23
  • 1
  • 6