title: Comparing an int to its old value in Java
body: <p>I'm writing some code where I need to determine by how much an integer has changed in the last loop. </p>
  
  <p>My Code:</p>
  
  <pre><code>import gnu.io.CommPort;
  import gnu.io.CommPortIdentifier;
  import gnu.io.SerialPort;
  import com.leapmotion.leap.*;
  
  import java.io.IOException;
  
  import com.leapmotion.leap.Gesture.State;
  import java.io.OutputStream;
  
  import com.leapmotion.leap.Controller;
  import com.leapmotion.leap.Frame;
  import com.leapmotion.leap.Listener;
  import java.lang.Math;
  
  class SampleListenerMain extends Listener {
  
  public void onInit(Controller controller) {
  System.out.println("Initialized");
  }
  
  public void onConnect(Controller controller) {
  System.out.println("Connected");
  controller.enableGesture(Gesture.Type.TYPE_SWIPE);
  controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
  controller.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);
  controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
  
  
  }
  
  public void onDisconnect(Controller controller) {
  System.out.println("Disconnected");
  }
  
  public void onExit(Controller controller) {
  System.out.println("Exited");
  }
  public void onFrame(Controller controller) {
  
  
  Frame frame = controller.frame();
  
  Hand hand = frame.hands().rightmost();
  float positionx = hand.palmPosition().getX();
  System.out.println("X: " + positionx);
  
  float positiony = hand.palmPosition().getY();
  System.out.println("Y:" + positiony);
  
  float positionz = hand.palmPosition().getZ();
  System.out.println("Z: " + positionz);
  
  System.out.println("Hands: " + frame.hands().count());
  
  int in_min = -117;
  int in_max = 117;
  int out_min = 900;
  int out_max = 1900;
  int in_min2 = -73;
  int in_max2 = 73;
  
  int positionmapx = (int) ((positionx - in_min) * (out_max - out_min) /     (in_max - in_min)) + out_min;
  int positionmapy = (int) ((positiony - in_min) * (out_max - out_min) / (in_max - in_min)) + out_min;
  int positionmapz = (int) ((positionz - in_min) * (out_max - out_min) / (in_max2 - in_min)) + out_min;
  
  
  if (frame.hands().count() == 0) {
      LeapDrone.writeToArduino("0");
  }
  
  
  int posmapxold = 0;
  int posmapyold = 0;
  int posmapzold = 0;
  
  
  String data = Integer.toString(positionmapy)+":"+     Integer.toString(positionmapx)+":"+Integer.toString(positionmapz);
  
  System.out.println("data: " + data);
  
  
  System.out.println("Old X: " + posmapxold);
  
  System.out.println("Difference X: " + Math.abs(positionmapy - posmapyold));
  
  if (Math.abs(positionmapx - posmapxold) &gt;= 25) {
  
  
      LeapDrone.writeToArduino(data);
  
      try {
          Thread.sleep(50);
      } catch (InterruptedException e) {
  
          e.printStackTrace();
      }
  
  }
  
  if (Math.abs(positionmapy - posmapyold) &gt;= 25) {
  
  
      LeapDrone.writeToArduino(data);
  
      try {
          Thread.sleep(50);
      } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
  }
  
  if (Math.abs(positionmapz - posmapzold) &gt;= 25) {
  
  
      LeapDrone.writeToArduino(data);
  
      try {
          Thread.sleep(50);
      } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
  }
  
  posmapxold = positionmapx;
  posmapyold = positionmapy;
  posmapzold = positionmapz;
  
  System.out.println("New-Old X: " + posmapxold);
  
  try {
      Thread.sleep(100);
  } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }
  
  GestureList gestures = frame.gestures();
  for (int i = 0; i &lt; gestures.count(); i++) {
      Gesture gesture = gestures.get(i);
  
  
  
  }
  }
  }
  
     class LeapDrone {
  static OutputStream out = null;
  
  public static void main(String[] args) {
  //Connect to Arduino BT
  
  try
  {
  (new LeapDrone()).connect("/dev/cu.usbmodem1421");
  }
     catch ( Exception e )
  {
      e.printStackTrace();
  System.exit(0);
  }
  
  // Create a sample listener and controller
  SampleListenerMain listener = new SampleListenerMain();
  Controller controller = new Controller();
  
  // Have the sample listener receive events from the controller
  controller.addListener(listener);
  // Keep this process running until Enter is pressed
  System.out.println("Press Enter to quit...");
  try {
  System.in.read();
  } catch (IOException e) {
  e.printStackTrace();
  }
  
  // Remove the sample listener when done
  controller.removeListener(listener);
  }
  
  void connect ( String portName ) throws Exception {
  
  CommPortIdentifier portIdentifier =     CommPortIdentifier.getPortIdentifier(portName);
  if ( portIdentifier.isCurrentlyOwned() )
  {
  System.out.println("Error: Port is currently in use");
  }
  else
  {
  CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
  
  if ( commPort instanceof SerialPort )
  {
  SerialPort serialPort = (SerialPort) commPort;
  serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,
  SerialPort.PARITY_NONE);
  out = serialPort.getOutputStream();
  }
  else
  {
  System.out.println("Error: Only serial ports are handled by this example.");
  }
  }
  }
  
  public static void writeToArduino(String data)
  {
  String tmpStr = data;
  byte bytes[] = tmpStr.getBytes();
  try {
  out.write(bytes);
  } catch (IOException e) { }
  }
  }
  </code></pre>
  
  <p>For example, I have integer i. I want to run a loop and see how much i has changed by (In my case, i is assigned to an x-coord that changes every loop). I set integer j equal to i once i has been set in that loop. I then need to compare i to j.</p>
  
  <p>The issue is that I have to define j as equal to something to initialize it. That means that if, for example, I initialize j by setting it equal to 0, it resets to 0 every time my code loops, which means that I can't compare i to its old value (which is supposed to be j)......</p>
  
  <p>I tried using a 'for' statement, but I get errors because later in my code when I compare i and j it says that j hasn't been initialized.... I suppose because code in the for loop doesn't necessarily get run in the beginning of the program?</p>
  
  <p>Sorry for the confusing question.... It's hard for me to explain!</p>
  
  <p>Any help is much appreciated!!!</p>