0

I have a code in which there is a variable v whose default value is 0 . I also have 2 buttons : ok and nope. Using my code, when ok is pressed, the value of v should be 1 and when nope is pressed the value of v should be 2. But in both the cases it is printing the value of v as 2. Why is this? How can I correct it?

Edit : I did a minor mistake, I have corrected it, thanks to MadProgrammer.

import java.awt.*;
public class chk extends java.applet.Applet
{
    Button ok = new Button("OK!");
    Button nope = new Button("Nope");
    int v = 0;
    public void init()
    {
        setBackground(Color.white);
        add(ok);
        add(nope);
    }

    public boolean action(Event evt , Object arg)
    {
        if(evt.target instanceof Button)
        {
            check((Button)evt.target);
            return true;
        }
        return false;
    }
    public void check(Button b)
    {
        if(b == ok);
        {
            v= 1;
            repaint();
        }
        if(b == nope);
        {
            v=2;
            repaint();
        }
   }
   public void paint(Graphics g)
   {
        g.drawString(""+v,40,40);
   }
   }

All help is appreciated. Please let me know if I've made any mistakes.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Darsh
  • 27
  • 8
  • Wow, I've not seen that style of code for...a very long time. Any reason why you're using AWT over Swing (or even FX)? – MadProgrammer Jan 21 '14 at 11:21
  • 1
    1. did you debug check() ? 2. reference comparison (`b == ok`) is little dangerous – sanbhat Jan 21 '14 at 11:21
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Jan 21 '14 at 12:30

3 Answers3

6

Take a look at your if statements

    if(b == nope);

You've added a ; at the end of each statement. This means that both blocks of code within the {...} braces are being executed regardless of what the if statements might want. Remove the ; from the end of each statement

Ps- Unless you have a very particular need to use raw AWT, I'd consider taking a look at Swing or if you're really adventurous, JavaFX, as they are more modern GUI APIs

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    @MadProgrammer, wow! I have been reviewing this code for a minute and have not seen this! – AlexR Jan 21 '14 at 11:25
  • 2
    @AlexR I'm not sure why it stood it, but it just did :P – MadProgrammer Jan 21 '14 at 11:27
  • @MadProgrammer, i really appreciate ur eyes , i have been studding java from 3 months , but i never did this kind of mistake and therefore never gave much attention to it , thanz a lot , i had created a much lengthier program and did the same mistake , my problem which i was not able to solve from 3 days has been solved :) – Darsh Jan 21 '14 at 11:31
  • @Darsh If you are satisfied with MadProgrammer's answer, you really should accept it by hitting the little green checkbox. – CodeChimp Jan 21 '14 at 13:10
0

Don't use deprecated method and use an ActionListener instead :

ok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                v = 1;
                repaint();
            }
});
nope.addActionListener(new ActionListener() {               
            @Override
            public void actionPerformed(ActionEvent arg0) {
                v = 2;
                repaint();
            }
});
user2336315
  • 15,697
  • 10
  • 46
  • 64
0

== COmparison is not considered good try something like this and As MadProgrammer said try removing ; after if and else.

if(b.getLabel().equals("OK!"))
     {
       v=1;
       repaint();
      }
if(b.getLabel().equals("Nope"))
      {
        v=2;
        repaint();
      }
Vinayak Pingale
  • 1,315
  • 8
  • 19