-1

NOTE: My English isn't the best so please don't mind too much Grammar mistakes.

Hey there, Java Starter here, anyways i was writing my CPS Test program, and i found a bug i can't figure out. this question may got asked before but i couldn't use the .equals function.

Code:

boolean wait = false;
times2 times2 = new times2(0, false);
times2.setTimes(0);
final AtomicInteger times = new AtomicInteger(0);

b1.addActionListener(new ActionListener() {
  final AtomicInteger clicks = new AtomicInteger(0);
  @SuppressWarnings("unlikely-arg-type")
  public void actionPerformed(ActionEvent e) {
    System.out.println("Console> startTest;");
    // This if condition won't work.
    if(times.equals(times2.getTimes())) {
      b1.setText("3");
      try {
        TimeUnit.SECONDS.sleep((long)1.0);
      } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
      b1.setText("2");
      try {
        TimeUnit.SECONDS.sleep((long)1.0);
      } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
      b1.setText("1");
      try {
        TimeUnit.SECONDS.sleep((long)1.0);
      } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
      b1.setText("CLICK!");
      times.incrementAndGet();
      times2.setWait(true);
    }else if(!times.equals(times2.getTimes())){
      clicks.incrementAndGet();
    }
    if(times2.getWait() == true) {
      try {
        TimeUnit.SECONDS.sleep((long)10.0);
      } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
      JLabel text2 = new JLabel("");

      Font f = new Font(null, Font.BOLD , 0);

      Font size = f.deriveFont(45f);
      double clicks2 = clicks.get();
      double results = clicks2/10;
      text2.setText("<html> Your final Results: <html> " +  "<html> <br> <html>" + results);
      text2.setFont(size);
      text2.setPreferredSize(new Dimension(100,100));

      JFrame end = new JFrame();
      end.setPreferredSize(new Dimension(350,350));
      end.setMaximumSize(new Dimension(450,450));
      end.setLocationRelativeTo(null);
      end.setTitle("RESULTS");
      end.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      end.pack();
      end.setVisible(true);
      end.getContentPane().add(text2, BorderLayout.CENTER);
    }
  }
});

times2.java:

public class times2 {
    private int times;
    private boolean wait;

    public times2(int times2, boolean wait2) {
      this.times = times2;
      this.wait = wait2;
    }

    public int getTimes() {
      return times;
    }

    public void setTimes(int times) {
      this.times = times;
    }

    public boolean isWait() {
      return wait;
    }

    public void setWait(boolean wait) {
      this.wait = wait;
    }

    public boolean getWait() {
      // TODO Auto-generated method stub
      return false;
    }
}

If you know whats wrong Please respond to this post.

Taylor Hx
  • 2,815
  • 23
  • 36
  • 1
    you did not override equals method. look [here](https://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java) – XtremeBaumer Aug 08 '17 at 06:11
  • 1
    This isn't really relevant to the question but in Java classes are usually capitalized so times2 should be Times2. – Acontz Aug 08 '17 at 06:12

2 Answers2

2

If you want for it to work you need to compare like so:

times.get().equals(times2.getTimes())

or (java 7++)

Objects.equals(times.get(), times2.getTimes())

time is an instance of AtomicInteger, times2.getTime() is an int, casted to Integer.

Those are two different classes.

So to solve it you need to bring both to same type, simply by times.get(), which will produce an int.

Beri
  • 11,470
  • 4
  • 35
  • 57
2

You should overwrite equals method

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    test2 test2 = (test2) o;

    if (times != test2.times) return false;
    return wait == test2.wait;

}