1

I have some logic issue in Couchbase Lite. Here is my db data like below

"3 step", 50.00, 4, "step"
"3 step", 50.00, 3, "step"
"3 step", 50.00, 3, "step"
"3 step", 50.00, 4, "step"
"3 step", 50.00, 7, "step"
"3 step", 50.00, 2, "step"
"3 step", 50.00, 2, "step"
"3 step", 50.00, 1, "step"
"3 step", 50.00, 2, "step"
"3 step", 50.00, 3, "step"

4,3,3,4,7 this single step of the day 4 is a start step 7 is a end step.

which ever > than 5 is end step very next one is start step how can i can get only start step and end step value?

i tried with like below

 while (rowEnum.hasNext()) {
     Document gpsData = rowEnum.next().getDocument();
     int stepDiff = (int) gpsData.getProperty("stepdiff");
     if (start){
         startStepArray.add((String) gData.getProperty("stepsDiff"));
         start = false;
     }
     if (diffTime > 5) {
         endStepArray.add((String) gData.getProperty("stepsDiff"));
         start = true;
     }
}

But this logic will not work if there is no >5

can some one guide me how can i achieve this?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Gurumoorthy Arumugam
  • 2,129
  • 1
  • 25
  • 40
  • Shouldn't you be checking `if (stepDiff > 5)`? Otherwise, what is `diffTime`? – OneCricketeer Dec 14 '15 at 17:17
  • Also, you have `gData` instead of `gpsData` and `getProperty("stepsDiff")` instead of `getProperty("stepdiff")`... Basically, it is hard to tell what you are trying to do without a little more information, such as the schema of your database. – OneCricketeer Dec 14 '15 at 17:19

1 Answers1

0

I'm not entirely sure what your problem is, but just throwing this simple example together seems to work fine.

Data wrapper object

class Document {
    private String s1, s4;
    private double d2;
    private int i3;

    public Document(String arg0, double arg1, int arg2, String arg3) {
        this.s1 = arg0;
        this.d2 = arg1;
        this.i3 = arg2;
        this.s4 = arg3;
    }

    public int getStepDiff() {
        return this.i3;
    }
}

Driver class

public static void main(String[] args) {
    List<Document> lst = new ArrayList<>();
    lst.add(new Document("3 step", 50.00, 4, "step"));
    lst.add(new Document("3 step", 50.00, 3, "step"));
    lst.add(new Document("3 step", 50.00, 3, "step"));
    lst.add(new Document("3 step", 50.00, 4, "step"));
    lst.add(new Document("3 step", 50.00, 7, "step"));
    lst.add(new Document("3 step", 50.00, 2, "step"));
    lst.add(new Document("3 step", 50.00, 2, "step"));
    lst.add(new Document("3 step", 50.00, 1, "step"));
    lst.add(new Document("3 step", 50.00, 2, "step"));
    lst.add(new Document("3 step", 50.00, 3, "step"));
    Iterator<Document> rowEnum = lst.iterator();

    /** Your logic starts here **/
    boolean start = true;
    List<String> startStepArray = new ArrayList<>();
    List<String> endStepArray = new ArrayList<>();

    while (rowEnum.hasNext()) {
        Document gpsData = rowEnum.next();
        int stepDiff = gpsData.getStepDiff();

        if (start) {
            startStepArray.add(String.valueOf(gpsData.getStepDiff()));
            start = false;
        }

        if (stepDiff > 5) {
            endStepArray.add(String.valueOf(gpsData.getStepDiff()));
            start = true;
        }
    }

    System.out.println(startStepArray); // [4, 2]
    System.out.println(endStepArray); // [7]
}

If all the data were less than or equal to 5 (say the 7 was a 3) and you wanted the very last document to be an end-step, you could change this one line to do that.

if (stepDiff > 5 || !rowEnum.hasNext()) {

Then you would have a start-step of [4], and an end-step of [3]. Or, keep the 7, and the start-steps would be [4, 2] and end-steps would be [7, 3].

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245