Ali Baker
Ali Baker

Reputation: 89

Compare the current value with previous value in a loop in java

How do I compare the current value with previous value in for loop in java and determine if they are different?

Here I have for loop that loop every time I click the mouse. Therefore i depends on where I click.

Here both the current i and the previous i are equal to 3.

https://www.dropbox.com/s/55dao9a9r8o4yis/Untitled%20picture%206.png

Here the current i equal to 7 and the previous i equal to 11.

https://www.dropbox.com/s/pj38l23ughn6ey9/Untitled%20picture%207.png

I am trying to make if statement to compare the two values of previous i with the current i.

Here is my for loop

public void mousePressed() {      
    if (state == 0) {            
        for (int i = 0; i < 6; i++) {
            if (mouseX >= cards[i].x && mouseX <= cards[i].x + cards[i].WIDTH && mouseY >= cards[i].y && mouseY <= cards[i].y + cards[i].HEIGHT) {
                cards[i].flip();                     
            }               
        }

Upvotes: 3

Views: 24498

Answers (3)

Ritu Shikha
Ritu Shikha

Reputation: 1

 Map<String, String> previousRow = lines.next();
    while (lines.hasNext()) {
        Map<String, String> row = lines.next();

        if (previousRow.get(key).equals(row.get(key)))
        {
            System.out.println("Found Duplicate Product " + row);
        }
        previousRow = row;

Upvotes: 0

pasha701
pasha701

Reputation: 7207

Start "i" with "1", smth. like:

    for (int i = 1; i < 6; i++) {
        if (cards[i].equals(cards[i-1])) {
            cards[i].flip();                     
        }  
    }

Upvotes: 1

Dark Knight
Dark Knight

Reputation: 8347

This is what you are looking for

int currentValue=0;
    int lastValue=0;

    for(int i=0;i<10;i++){
        currentValue =i;

        if(i>0){
            System.out.println(" Current Value is "+currentValue+" Last Value is "+lastValue);
        }   

        lastValue=currentValue;
    }

Upvotes: 1

Related Questions