2

I am trying to make a simple addition using java web services. I have a problem getting the input from the JTextField into my ints so that the program can carry out the addition. The code I have is below. When the user inputs the first value they should click the first button (butta) same with the second, once the second is clicked it should show somewhere the result. The integer parsing doesnt seem to work when I click on the buttons its not taking the value.

 public class Wscalcclient {
           static int aa=0, bb=0, cc=0;
        String str = new String(); 
        JTextField fielda, fieldb;
        JTextField fieldc;

        Wscalcclient()
        {  JButton butta, buttb;
           JLabel result;
           JPanel panel;

           JFrame frame  = new JFrame();
           frame.setBackground(Color.white);
           frame.setForeground(Color.black);
           frame.setSize(500,250);

           panel = new JPanel();
           panel.setBackground(Color.cyan);
           panel.setForeground(Color.black);
           panel.setLayout(new GridLayout(0,2));

           butta = new JButton("enter number a");
           buttb = new JButton("enter number b");
           result = new JLabel("result a+b=");
           fielda = new JTextField();
           fieldb = new JTextField();
           fieldc = new JTextField();

           panel.add(butta);
           panel.add(fielda);
           panel.add(buttb);
           panel.add(fieldb);
           panel.add(result);
           panel.add(fieldc);

           butta.addActionListener(new GetA());
           buttb.addActionListener(new GetB());

           frame.getContentPane().add(panel);
           frame.pack();
           frame.setVisible(true);

        }

        class GetA implements ActionListener
        { public void actionPerformed(ActionEvent t1e)
          { aa = Integer.parseInt(fielda.getText()); }
        }

        class GetB implements ActionListener
        { public void actionPerformed(ActionEvent t1e)
          { bb = Integer.parseInt(fieldb.getText()); }
        }

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            try
          { new Wscalcclient();
              calc.Calculator_Service service = new calc.Calculator_Service();
            calc.Calculator port = service.getCalculatorPort();


            BufferedReader keybrd = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("enter number a: ");
            int a = Integer.parseInt(keybrd.readLine());
            System.out.println("enter number b: ");
            int b= Integer.parseInt(keybrd.readLine());

            int cc = port.add(a,b);
            System.out.println("sum = " + cc);
          }
          catch (Exception ex)
          { System.out.println("exception = " + ex);
          }
        }
    }
kleopatra
  • 51,061
  • 28
  • 99
  • 211
ahb
  • 138
  • 11
  • Is this [tag:homework]? If not, I'd suggest using a single field and the [ScriptEngine](http://stackoverflow.com/a/7441804/418556). – Andrew Thompson Dec 23 '11 at 04:40
  • Hey can you paste the code where you are reading the input (i.e. the add() function) – Ankit Dec 23 '11 at 04:45
  • I love questions that assert that some basic function of Java is "not working". – Paul Dec 23 '11 at 04:48
  • Instead of posting the whlesale copy of code, just post the segment of code that is the actual cause of the problem. – Lion Dec 23 '11 at 04:56

1 Answers1

5

You are setting bb and aa in your ActionListeners. You never again examine ("read") those values. So how do you know they are getting the wrong value? You don't have any code that actually uses them.

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
  • What code do I need to use so that i get the value of aa to be assigned into a and the value of bb to be assigned into b. I have tried a lot of things but i just dont seem to get it right. Please help THANK YOU – ahb Dec 27 '11 at 21:15