0

Here i am not able to read the integer value,can anyone suggest me what is the problem in thios code.iam trying to write calculator code here step by stem i chewcking the eroor, while checkig the code whether it will take integer input from butten anfd textfield,here if i remove int cv=Integer.parseInt(t.getText()); code will work fine if add it it is giving error

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
 public class ca_today extends Applet implements ActionListener{
    TextField t = new TextField(5);
    String str[] = {"+","-","/","%","*","<-","1/x","=","C",".","MR","MS"};
    Button b[]= new Button[12];
    Button b1[]= new Button[10];
    Button exit = new Button("EXIT");
    String pv,op;

    public void init() {
        setBackground( Color.CYAN );
        t.setBackground(Color.yellow);              
        Panel p = new Panel();
        p.add(t);
        t.addActionListener(this);        
        p.setLayout(new GridLayout(3,6));
        for( int i = 0 ; i < 10; i++ ){
            b1[i]=new Button(""+i);
            p.add(b1[i]);       
            b1[i].addActionListener(this);          
        }
        for(int j = 0 ; j <12; j++ ){
            b[j]=new Button(str[j]);
            p.add(b[j]);        
            b[j].addActionListener(this);
        }   
        p.add(exit);
        exit.addActionListener(this);
        add(p); 
    }

    public void actionPerformed(ActionEvent a){
        int res=0;
        String c=a.getActionCommand();
        int cv=Integer.parseInt(t.getText());//takes the i/p from textarea
        t.setText(""+cv);
        }
}
Carl0s1z
  • 4,683
  • 7
  • 32
  • 47
  • What is your input? give a sample of t.getText() – saravanakumar Apr 03 '14 at 11:51
  • What error do you get?? Post the stacktrace. – Dropout Apr 03 '14 at 11:59
  • 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 Apr 04 '14 at 05:08

1 Answers1

0

It is better to add like this

    public void actionPerformed(ActionEvent a){
    int res=0;
    String c=a.getActionCommand();
    t.setText(""+t.getText());
    }
saravanakumar
  • 1,747
  • 4
  • 20
  • 38