hi everyone :)
I have a problem here >>which I can't add 2 numbers >> and I don't know what is the solution for that !!>>it always gives me an empty result !and that is because (num2) always parse a (null) value ! so how can I edit the value of num2 then !
(the code that I want to do here is to >>first take the first num then setText(" ")>>then enter the seconde num >> then parse it >>then do the summation of these numbers !)
here is the code !

import java.lang.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Calc extends JFrame{
private JButton B_9,B_8,B_7,B_6,B_5,B_4;
private JButton B_3,B_2,B_1,B_0,B_add,B_equal;
private JTextField T;
private String temp=" ",tempans=" ";
public Calc(){
    super("Calculator");
    setLayout(null);
    T=new JTextField("0");
    T.setHorizontalAlignment(FlowLayout.TRAILING);
    T.setSize(360,30);
    T.setLocation(40,20);
     add(T);
      B_9=new JButton("9");
     B_9.setSize(60,30);
     B_9.setLocation(190,260);
     add( B_9);

     B_8=new JButton("8");
     B_8.setSize(60,30);
     B_8.setLocation(115,260);
     add( B_8);

     B_7=new JButton("7");
     B_7.setSize(60,30);
     B_7.setLocation(40,260);
     add( B_7);

     B_6=new JButton("6");
     B_6.setSize(60,30);
     B_6.setLocation(190,300);

     add( B_6);

     B_5=new JButton("5");
     B_5.setSize(60,30);
     B_5.setLocation(115,300);
     add( B_5);

     B_4=new JButton("4");
     B_4.setSize(60,30);
     B_4.setLocation(40,300);
     add(  B_4);

     B_3=new JButton("3");
     B_3.setSize(60,30);
     B_3.setLocation(190,340);
     add( B_3);

     B_2=new JButton("2");
     B_2.setSize(60,30);
     B_2.setLocation(115,340);
     add( B_2);

     B_1=new JButton("1");
     B_1.setSize(60,30);
     B_1.setLocation(40,340);
     add( B_1);

     B_0=new JButton("0");
     B_0.setSize(60,30);
     B_0.setLocation(40,380);
     add( B_0); 

     B_add=new JButton("+");
     B_add.setSize(60,30);
     B_add.setLocation(265,340);
     add(B_add);
     B_equal=new JButton("=");
     B_equal.setSize(60,30);
     B_equal.setLocation(340,380);
     add( B_equal);

        Inner Handler=new Inner ();

     B_9.addActionListener(Handler);
     B_8.addActionListener(Handler);
     B_7.addActionListener(Handler);
     B_6.addActionListener(Handler);
     B_5.addActionListener(Handler);
     B_4.addActionListener(Handler);
     B_3.addActionListener(Handler);
     B_2.addActionListener(Handler);
     B_1.addActionListener(Handler);
     B_0.addActionListener(Handler);
      B_equal.addActionListener(Handler);
     B_add.addActionListener(Handler);

     }
     private class Inner implements ActionListener {
public void actionPerformed (ActionEvent E){
    String out=T.getText();
    String String_output;
    if (E.getSource()==B_9){
        String s=new String ();
        s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"9");

                }
                else {
                    int num=Integer.parseInt(out);
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"9");

                }


        }
        else {
            T.setText("0");
            T.setText("9");
            tempans="9";

        }

    }   
    if (E.getSource()==B_8){
        String s=new String ();
        s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"8");
                }
                else {
                    int num=Integer.parseInt(out);
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"8");

                }


        }
        else {
            T.setText("0");
            T.setText("8");
            tempans="8";


        }
    }   
    if (E.getSource()==B_7){
        String s=new String ();
        s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"7");

                }
                else {
                    int num=Integer.parseInt(out);
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"7");

                }


        }
        else {
            T.setText("0");
            T.setText("7");

        }
    }
    if (E.getSource()==B_6){
        String s=new String ();
        s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"6");

                }
                else {
                    int num=Integer.parseInt(out);
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"6");

                }


        }
        else {
            T.setText("0");
            T.setText("6");

        }
    }   
    if (E.getSource()==B_5){
        String s=new String ();
        s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"5");

                }
                else {
                    int num=Integer.parseInt(out);
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"5");

                }


        }
        else {
            T.setText("0");
            T.setText("5");

        }
    }   
    if (E.getSource()==B_4){
        String s=new String ();
        s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"4");

                }
                else {
                    int num=Integer.parseInt(out);
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"4");

                }


        }
        else {
            T.setText("0");
            T.setText("4");

        }
    }   

    if (E.getSource()==B_3){
        String s=new String ();
        s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"3");

                }
                else {
                    int num=Integer.parseInt(out);
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"3");

                }


        }
        else {
            T.setText("0");
            T.setText("3");

        }
    }   
    if (E.getSource()==B_2){
        String s=new String ();
        s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"2");

                }
                else {
                    int num=Integer.parseInt(out);
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"2");

                }


        }
        else {
            T.setText("0");
            T.setText("2");

        }
    }


    if (E.getSource()==B_1){

        String s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"1");

                }
                else {

                    int num=Integer.parseInt(out);
                    //temp +=num;
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"1");


                }


        }
        else {
            T.setText("0");
            //temp="1";
            T.setText("1");

        }
    }   
    if (E.getSource()==B_0){
        String s=new String ();
        s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    //T.setText("0");
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"0");

                }
                else {
                    int num=Integer.parseInt(out);
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"0");

                }


        }
        else {
            T.setText("0");
            T.setText("0");

        }
    }   

if (E.getSource()==B_add){
        Double number=Double.parseDouble(out);
        double addition=0;
        double number2;
        T.setText(" ");

        number2=Double.parseDouble(tempans);
        System.out.println("n"+number2);
        addition = (number+number2);
        String resulte=Double.toString(addition);
        temp+=resulte;
        String_output=B_add.getActionCommand();
    }//fun add

 if (E.getSource()==B_equal){

        T.setText(temp);

    }

    public static void main(String[] args) {
        Calc v=new Calc();
        v.setSize(450,500);
        v.setVisible(true);
        v.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }
    }}

Two remarks:

  1. Be more specific. when is it throwing an exception, on which line, during which part of the flow?
  2. Don't just copy paste code here that resembles code that you are running, show us the actual code.

I know this is not the code you tried, because this code simply won't compile.
To give an example:

if (E.getSource()==B_equal){
        T.setText(temp);
    }
    public static void main(String[] args) {
        Calc v=new Calc();
        v.setSize(450,500);
        v.setVisible(true);
        v.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

either that if statement is outside of a method, and won't allow the code to be compiled, OR you are trying to write a main method within another method, which won't allow your code to be compiled.

@stultuske
this is the complete code !
and the main >>I put it in another file but in the same package !
and there is no exception >>but the prob. here is that I can't add 2 numbers ! and I don't know another way to do so !
all the time I press for example on (the (9) button )it shows in the textfield (9) then when I press on the (+)buttom it makes the textfield empty >>after that when I press on the (the (8) button )it shows in the textfield (8) then when I press on the (=)buttom ..it shows the result = 18 !!
so number 2 takes num 9 not number 8 !!
so the addition is wrong ! plz help me :(

import java.lang.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Calc extends JFrame{
private JButton B_On,B_Shift,B_exp,B_Log,Btan_1,Bcos_1;
private JButton Bsin_1,B_sqrt,B_tan,B_sin,B_cos,Bsinch;
private JButton B_cosh, B_tansh,Bfactorial,BInverseOfX,BAc,BDel;
private JButton B_9,B_8,B_7,B_6,B_5,B_4;
private JButton B_3,B_2,B_1,B_0,B_Multi,B_Div;
private JButton B_add,B_sub,B_equal,B_answ,B_dot,B_power;
private JButton B_abs,B_Topower;

private JTextField T;
private String temp=" ",tempans=" ";

public Calc(){
    super("Calculator");
    setLayout(null);
    T=new JTextField("0");
    T.setHorizontalAlignment(FlowLayout.TRAILING);
    T.setSize(360,30);
    T.setLocation(40,20);
     add(T);
     B_On=new JButton("ON");
     B_On.setSize(60,30);
     B_On.setLocation(340,60);
     add(B_On);
     B_Shift=new JButton("Shift");
     B_Shift.setSize(60,30);
     B_Shift.setLocation(40,60);
     add(B_Shift);
     B_exp=new JButton("exp");
     B_exp.setSize(60,30);
     B_exp.setLocation(340,100);
     add(B_exp);
     B_Log=new JButton("Log");
     B_Log.setSize(70,30);
     B_Log.setLocation(240,100);
     add(B_Log);
     Btan_1=new JButton("tan-1");
     Btan_1.setSize(70,30);
     Btan_1.setLocation(240,140);
     add(Btan_1);
     Bcos_1=new JButton("cos-1");
     Bcos_1.setSize(70,30);
     Bcos_1.setLocation(140,140);
     add(Bcos_1);
     Bsin_1=new JButton("sin-1 ");
     Bsin_1.setSize(70,30);
     Bsin_1.setLocation(40,140);
     add(Bsin_1);
     B_sqrt=new JButton("√");
     B_sqrt.setSize(60,30);
     B_sqrt.setLocation(340,180);
     add(B_sqrt);
     B_tan=new JButton("Tan");
     B_tan.setSize(70,30);
     B_tan.setLocation(240,180);
     add(B_tan);
     B_sin=new JButton("Sin");
     B_sin.setSize(70,30);
     B_sin.setLocation(140,180);
     add(B_sin);
     B_cosh=new JButton("Cosh");
     B_cosh.setSize(70,30);
     B_cosh.setLocation(40,180);
     add( B_cosh);
     Bsinch=new JButton("sinch");
     Bsinch.setSize(70,30);
     Bsinch.setLocation(140,100);
     add( Bsinch);
     B_cos=new JButton("cos");
     B_cos.setSize(60,30);
     B_cos.setLocation(340,140);
     add( B_cos);
     B_tansh=new JButton("tansh");
     B_tansh.setSize(70,30);
     B_tansh.setLocation(40,100);
     add( B_tansh);
     Bfactorial=new JButton("X!");
     Bfactorial.setSize(60,30);
     Bfactorial.setLocation(340,220);
     add( Bfactorial);
     BInverseOfX=new JButton("1/x");
     BInverseOfX.setSize(70,30);
     BInverseOfX.setLocation(240,220);
     add( BInverseOfX);
     B_abs=new JButton("| |");
     B_abs.setSize(70,30);
     B_abs.setLocation(140,220);
     add( B_abs);

     B_Topower=new JButton("^");
     B_Topower.setSize(70,30);
     B_Topower.setLocation(40,220);
     add( B_Topower);

     BAc=new JButton("Ac");
     BAc.setSize(60,30);
     BAc.setLocation(340,260);
     add( BAc);
     BDel=new JButton("Del");
     BDel.setSize(60,30);
     BDel.setLocation(265,260);
     add( BDel);
     B_9=new JButton("9");
     B_9.setSize(60,30);
     B_9.setLocation(190,260);
     add( B_9);

     B_8=new JButton("8");
     B_8.setSize(60,30);
     B_8.setLocation(115,260);
     add( B_8);

     B_7=new JButton("7");
     B_7.setSize(60,30);
     B_7.setLocation(40,260);
     add( B_7);

     B_6=new JButton("6");
     B_6.setSize(60,30);
     B_6.setLocation(190,300);

     add( B_6);

     B_5=new JButton("5");
     B_5.setSize(60,30);
     B_5.setLocation(115,300);
     add( B_5);

     B_4=new JButton("4");
     B_4.setSize(60,30);
     B_4.setLocation(40,300);
     add(  B_4);

     B_3=new JButton("3");
     B_3.setSize(60,30);
     B_3.setLocation(190,340);
     add( B_3);

     B_2=new JButton("2");
     B_2.setSize(60,30);
     B_2.setLocation(115,340);
     add( B_2);

     B_1=new JButton("1");
     B_1.setSize(60,30);
     B_1.setLocation(40,340);
     add( B_1);

     B_0=new JButton("0");
     B_0.setSize(60,30);
     B_0.setLocation(40,380);
     add( B_0);

     B_Multi=new JButton("*");
     B_Multi.setSize(60,30);
     B_Multi.setLocation(265,300);
     add(B_Multi);

     B_Div=new JButton("/");
     B_Div.setSize(60,30);
     B_Div.setLocation(340,300);
     add( B_Div);

     B_add=new JButton("+");
     B_add.setSize(60,30);
     B_add.setLocation(265,340);
     add(B_add);

     B_sub=new JButton("-");
     B_sub.setSize(60,30);
     B_sub.setLocation(340,340);
     add( B_sub);

     B_equal=new JButton("=");
     B_equal.setSize(60,30);
     B_equal.setLocation(340,380);
     add( B_equal);

     B_answ=new JButton("Ans");
     B_answ.setSize(60,30);
     B_answ.setLocation(265,380);
     add(B_answ);


     B_dot=new JButton(".");
     B_dot.setSize(60,30);
     B_dot.setLocation(115,380);
     add( B_dot);

     B_power=new JButton("*10^");
     B_power.setSize(60,30);
     B_power.setLocation(190,380);
     add(B_power);

    Inner Handler=new Inner ();
    B_On.addActionListener(Handler);
    B_Shift.addActionListener(Handler);
    B_exp.addActionListener(Handler);
    B_Log.addActionListener(Handler);

    Btan_1.addActionListener(Handler);
    Bcos_1.addActionListener(Handler);
    Bsin_1.addActionListener(Handler);
    B_sqrt.addActionListener(Handler);
    B_tan.addActionListener(Handler);
    B_sin.addActionListener(Handler);
    B_cos.addActionListener(Handler);
    Bsinch.addActionListener(Handler);
    B_cosh.addActionListener(Handler);
    B_tansh.addActionListener(Handler);
    Bfactorial.addActionListener(Handler);
    BInverseOfX.addActionListener(Handler);
    BAc.addActionListener(Handler);
    BDel.addActionListener(Handler);
     B_9.addActionListener(Handler);
     B_8.addActionListener(Handler);
     B_7.addActionListener(Handler);
     B_6.addActionListener(Handler);
     B_5.addActionListener(Handler);
     B_4.addActionListener(Handler);
     B_3.addActionListener(Handler);
     B_2.addActionListener(Handler);
     B_1.addActionListener(Handler);
     B_0.addActionListener(Handler);
     B_Multi.addActionListener(Handler);
     B_Div.addActionListener(Handler);
     B_add.addActionListener(Handler);
     B_sub.addActionListener(Handler);
     B_equal.addActionListener(Handler);
     B_answ.addActionListener(Handler);
     B_dot.addActionListener(Handler);
     B_power.addActionListener(Handler);
     B_abs.addActionListener(Handler);
     B_Topower.addActionListener(Handler);

}
private class Inner implements ActionListener {
public void actionPerformed (ActionEvent E){
    String out=T.getText();
    String String_output;
    //String result_out=new String ();

    if (E.getSource()==B_tansh){
        double num=Double.parseDouble(out);
        double result=Math.tanh(num);
        String result_string=Double.toString(result);
        T.setText(result_string);
    }

    if (E.getSource()==B_cosh){
        double num=Double.parseDouble(out);
        double result=Math.cosh(num);
        String result_string=Double.toString(result);
        T.setText(result_string);
    }
    if (E.getSource()==Bsinch){
        double num=Double.parseDouble(out);
        double result=Math.sinh(num);
        String result_string=Double.toString(result);
        T.setText(result_string);
    }
    if (E.getSource()==B_tan){
        double num=Double.parseDouble(out);
        double result=Math.tan(num);
        String result_string=Double.toString(result);
        T.setText(result_string);
    }
    if (E.getSource()==B_sin){
        double num=Double.parseDouble(out);
        double result=Math.sin(num);
        String result_string=Double.toString(result);
        T.setText(result_string);
    }
    if (E.getSource()==B_cos){
        double num=Double.parseDouble(out);
        double result=Math.cos(num);
        String result_string=Double.toString(result);
        T.setText(result_string);
    }

    if (E.getSource()==B_Log){
        double num=Double.parseDouble(out);
        double result=Math.log(num);
        String result_string=Double.toString(result);
        T.setText(result_string);
    }
    if (E.getSource()==B_exp){
        double num=Double.parseDouble(out);
        double result=Math.exp(num);
        String result_string=Double.toString(result);
        T.setText(result_string);
    }
    if (E.getSource()==Bcos_1){
        double num=Double.parseDouble(out);
        double result=Math.acos(num);
        String result_string=Double.toString(result);
        T.setText(result_string);
    }
    if (E.getSource()==Bsin_1){
        double num=Double.parseDouble(out);
        double result=Math.asin(num);
        String result_string=Double.toString(result);
        T.setText(result_string);
    }
    if (E.getSource()==Btan_1){
        double num=Double.parseDouble(out);
        double result=Math.atan(num);
        String result_string=Double.toString(result);
        T.setText(result_string);
    }
    if (E.getSource()==B_9){
        String s=new String ();
        s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"9");

                }
                else {
                    int num=Integer.parseInt(out);
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"9");

                }


        }
        else {
            T.setText("0");
            T.setText("9");

        }

    }   
    if (E.getSource()==B_8){
        String s=new String ();
        s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"8");
                }
                else {
                    int num=Integer.parseInt(out);
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"8");

                }


        }
        else {
            T.setText("0");
            T.setText("8");


        }
    }   
    if (E.getSource()==B_7){
        String s=new String ();
        s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"7");

                }
                else {
                    int num=Integer.parseInt(out);
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"7");

                }


        }
        else {
            T.setText("0");
            T.setText("7");

        }
    }
    if (E.getSource()==B_6){
        String s=new String ();
        s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"6");

                }
                else {
                    int num=Integer.parseInt(out);
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"6");

                }


        }
        else {
            T.setText("0");
            T.setText("6");

        }
    }   
    if (E.getSource()==B_5){
        String s=new String ();
        s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"5");

                }
                else {
                    int num=Integer.parseInt(out);
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"5");

                }


        }
        else {
            T.setText("0");
            T.setText("5");

        }
    }   
    if (E.getSource()==B_4){
        String s=new String ();
        s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"4");

                }
                else {
                    int num=Integer.parseInt(out);
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"4");

                }


        }
        else {
            T.setText("0");
            T.setText("4");

        }
    }   

    if (E.getSource()==B_3){
        String s=new String ();
        s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"3");

                }
                else {
                    int num=Integer.parseInt(out);
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"3");

                }


        }
        else {
            T.setText("0");
            T.setText("3");

        }
    }   
    if (E.getSource()==B_2){
        String s=new String ();
        s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"2");

                }
                else {
                    int num=Integer.parseInt(out);
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"2");

                }


        }
        else {
            T.setText("0");
            T.setText("2");

        }
    }


    if (E.getSource()==B_1){

        String s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"1");

                }
                else {

                    int num=Integer.parseInt(out);
                    //temp +=num;
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"1");


                }


        }
        else {
            T.setText("0");
            //temp="1";
            T.setText("1");

        }
    }   
    if (E.getSource()==B_0){
        String s=new String ();
        s=T.getText();
        if (!s.isEmpty()){
             char c[]=new char [10];
             c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   a=false;}
                }
                if (!a){
                    //T.setText("0");
                    String S=new String ();
                    S=T.getText();

                    T.setText(S+"0");

                }
                else {
                    int num=Integer.parseInt(out);
                    String result_string=Integer.toString(num);
                    T.setText(result_string+"0");

                }


        }
        else {
            T.setText("0");
            T.setText("0");

        }
    }   



    if (E.getSource()==B_dot){
        int count=0;
        String d=T.getText();
        if (!d.isEmpty()){

        for (int n=0 ;n<d.length();n++){
            if (d.indexOf(n)=='.'){
                count++;
            }
            if (count > 1){
                T.setText("Error");

            }
            else {  
            int num=Integer.parseInt(out);
            String result_string=Integer.toString(num);
            T.setText(result_string+".");
            }
            }

        }
        else {
            T.setText("0");
            T.setText(".");
        }



        }


    /*
    if (E.getSource()==B_sub){
        double num=Double.parseDouble(out);
        double num2=Double.parseDouble(out2);
        double result=num-num2;
        String result_string=Double.toString(result);
        T.setText(result_string);
    }

    if (E.getSource()==B_Div){
        double num=Double.parseDouble(out);
        double num2=Double.parseDouble(out2);
        double result=num/num2;
        String result_string=Double.toString(result);
        T.setText(result_string);
        //case for infinty
    }
    if (E.getSource()==B_Multi){
        double num=Double.parseDouble(out);
        double num2=Double.parseDouble(out2);
        double result=num*num2;
        String result_string=Double.toString(result);
        T.setText(result_string);
    }
    */
    if (E.getSource()==BAc){

        T.setText("0");
    }

    if (E.getSource()==BDel){
        String S=T.getText();
        if(!S.isEmpty()){
        StringBuilder Sb = new StringBuilder(S);
        int size=Sb.length();
        Sb.deleteCharAt(size-1);
        String result_string=new String(Sb);
        T.setText(result_string);}
        else {T.setText("0");}


    }
if (E.getSource()==B_sqrt){
    double num=Double.parseDouble(out);
    double result=Math.sqrt(num);
    String result_string=Double.toString(result);
    T.setText(result_string);

    }


    if (E.getSource()==B_add){
        Double number=Double.parseDouble(out);
        double addition=0;
        double number2;
        T.setText(" ");

                number2=Double.parseDouble(out);
                addition = number+number2;
                String resulte=Double.toString(addition);
                temp=resulte;



        //String_output=B_add.getActionCommand();

    }//fun add

    //catch(ArrayIndexOutOfBoundsException e) {}    
 if (E.getSource()==B_equal){

        T.setText(temp);

    }
 if (E.getSource()==B_abs){

     double num=Double.parseDouble(out);
        double result=Math.abs(num);
        String result_string=Double.toString(result);
        T.setText(result_string);
 }
 if (E.getSource()==BInverseOfX){

        double num=Double.parseDouble(out);
        double result=1/num;
        String result_string=Double.toString(result);
        T.setText(result_string);
 }
/* if (E.getSource()==B_Topower){

        double num=Double.parseDouble(out);
        double result=Math.pow(num,);
        String result_string=Double.toString(result);
        T.setText(result_string);
}*/
 if (E.getSource()==Bfactorial){
        int f=1;
        String s=T.getText();
        if (!s.isEmpty()){
               char c[]=new char [10];
                c=s.toCharArray();
                boolean a=true;
                for(int m=0;m<c.length;m++){
                    if(!Character.isDigit(c[m])){   
                        a=false;
                        }
                }

                if (!a ){

                    T.setText("Error");

                }
                else {
                    int num=Integer.parseInt(out);
                    for(int v=1;v<=num;v++){
                    f=f*v;
                    }
                    String result_string=Integer.toString(f);
                    T.setText(result_string);

                }


        }
        else {
            T.setText("0");
        }}}}}

_______________________
the main :

import javax.swing.JFrame;
public class Test {

    public static void main(String[] args) {
        Calc v=new Calc();
        v.setSize(450,500);
        v.setVisible(true);
        v.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

}

Edited 1 Year Ago by shahera.arafat

I'll have a look at it during my lunch break, but I'll first refactor it (quite a bit). minimizing the nr of lines 'll make it easier to read.

Shahera: I'm sorry to say, but I haven't had the time to debug your code. Basically, because it took me a while to filter out all the redundant code.

Below, you can find your code, which I refactored to be more compact and readable. It should be fully functional to what you have now. maybe I'll have time later to check on it, but I'm not sure it'll be this weekend.

Anyway, you can already check the changes I've made (and while trying to find the error, you now have about 500 lines of code less to go through)

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

    public class Calc extends JFrame implements ActionListener {

        private static final int BUTTON_SIZE2 = 30;
        private static final int BUTTON_SIZE1 = 60;

        public static void main(String[] args) {
        Calc v = new Calc();
        v.setSize(450, 500);
        v.setVisible(true);
        v.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        }

        private JTextField T;
        private String temp = " ", tempans = " ";

        private JButton createButton(String label, int size1, int loc1, int loc2) {
        JButton button = new JButton(label);
        button.setSize(size1, BUTTON_SIZE2);
        button.setLocation(loc1, loc2);
        button.addActionListener(this);
        return button;
        }

        private JButton createButton(String label, int loc1, int loc2) {
        return createButton(label, BUTTON_SIZE1, loc1, loc2);
        }

        public Calc() {
        super("Calculator");
        setLayout(null);
        T = new JTextField("0");
        T.setHorizontalAlignment(FlowLayout.TRAILING);
        T.setSize(360, 30);
        T.setLocation(40, 20);
        add(T);
        add(createButton("ON", 340, 60));
        add(createButton("Shift", 40, 60));
        add(createButton("exp", 340, 100));
        add(createButton("Log", 70, 240, 100));
        add(createButton("tan-1", 70, 240, 140));
        add(createButton("cos-1", 70, 140, 140));
        add(createButton("sin-1", 70, 40, 140));
        add(createButton("?", 340, 180));
        add(createButton("Tan", 70, 240, 180));
        add(createButton("Sin", 70, 140, 180));
        add(createButton("Cosh", 70, 40, 180));
        add(createButton("sinch", 70, 140, 100));
        add(createButton("cos", 340, 140));
        add(createButton("tansh", 70, 40, 100));
        add(createButton("X!", 340, 220));
        add(createButton("1/x", 70, 240, 220));
        add(createButton("| |", 70, 140, 220));
        add(createButton("^", 70, 40, 220));

        add(createButton("Ac", 340, 260));
        add(createButton("Del", 265, 260));

        add(createButton("9", 190, 260));
        add(createButton("8", 115, 260));
        add(createButton("7", 40, 260));
        add(createButton("6", 190, 300));
        add(createButton("5", 115, 300));
        add(createButton("4", 40, 300));
        add(createButton("3", 190, 340));
        add(createButton("2", 115, 340));
        add(createButton("1", 40, 340));
        add(createButton("0", 40, 380));

        add(createButton("*", 265, 300));
        add(createButton("/", 340, 300));
        add(createButton("+", 265, 340));
        add(createButton("-", 340, 340));
        add(createButton("=", 340, 380));
        add(createButton("Ans", 265, 380));
        add(createButton(".", 115, 380));
        add(createButton("*10^", 190, 380));

        }

        @Override
        public void actionPerformed(ActionEvent E) {
        String event = ((JButton) E.getSource()).getText();
        // Added this validation, just in case
        if (event == null || event.isEmpty()) {
            System.out.println("NO EVENT RECEIVED");
            return;
        }
        String out = T.getText();
        String String_output;
        // String result_out=new String ();

        if (event.equals("tansh")) {
            T.setText(Double.toString(Math.tanh(Double.parseDouble(out))));
        } else if (event.equals("Cosh")) {
            T.setText(Double.toString(Math.cosh(Double.parseDouble(out))));
        } else if (event.equals("sinch")) {
            T.setText(Double.toString(Math.sinh(Double.parseDouble(out))));
        } else if (event.equals("Tan")) {
            T.setText(Double.toString(Math.tan(Double.parseDouble(out))));
        } else if (event.equals("Sin")) {
            T.setText(Double.toString(Math.sin(Double.parseDouble(out))));
        } else if (event.equals("cos")) {
            T.setText(Double.toString(Math.cos(Double.parseDouble(out))));
        } else if (event.equals("Log")) {
            T.setText(Double.toString(Math.log(Double.parseDouble(out))));
        } else if (event.equals("exp")) {
            T.setText(Double.toString(Math.exp(Double.parseDouble(out))));
        } else if (event.equals("cos-1")) {
            T.setText(Double.toString(Math.acos(Double.parseDouble(out))));
        } else if (event.equals("sin-1")) {
            T.setText(Double.toString(Math.asin(Double.parseDouble(out))));
        } else if (event.equals("tan-1")) {
            T.setText(Double.toString(Math.atan(Double.parseDouble(out))));
        } else if ("0123456789".contains(event)) {
            numberButton(event, out);
        } else if (event.equals(".")) {
            int count = 0;
            String d = T.getText();
            if (!d.isEmpty()) {
            for (int n = 0; n < d.length(); n++) {
                if (d.indexOf(n) == '.') {
                count++;
                }
                if (count > 1) {
                T.setText("Error");
                }
                else {
                T.setText(Integer.toString(Integer.parseInt(out)) + ".");
                }
            }
            }
            else {
            T.setText(".");
            }
        } else if (event.equals("Ac")) {
            T.setText("0");
        } else if (event.equals("Del")) {
            String S = T.getText();
            if (!S.isEmpty()) {
            StringBuilder Sb = new StringBuilder(S);
            int size = Sb.length();
            Sb.deleteCharAt(size - 1);
            String result_string = new String(Sb);
            T.setText(result_string);
            }
            else {
            T.setText("0");
            }

        } else if (event.equals("?")) {
            T.setText(Double.toString(Math.sqrt(Double.parseDouble(out))));
        } else if (event.equals("+")) {
            Double number = Double.parseDouble(out);
            double addition = 0;
            double number2;
            T.setText(" ");

            number2 = Double.parseDouble(out);
            addition = number + number2;
            String resulte = Double.toString(addition);
            temp = resulte;
        } else if (event.equals("=")) {
            T.setText(temp);
        } else if (event.equals("| |")) {
            T.setText(Double.toString(Math.abs(Double.parseDouble(out))));
        } else if (event.equals("1/x")) {
            T.setText(Double.toString(1 / Double.parseDouble(out)));
        } else if (event.equals("X!")) {
            int f = 1;
            String s = T.getText();
            if (!s.isEmpty()) {
            char c[] = new char[10];
            c = s.toCharArray();
            boolean a = true;
            for (int m = 0; m < c.length; m++) {
                if (!Character.isDigit(c[m])) {
                a = false;
                }
            }
            if (!a) {
                T.setText("Error");
            }
            else {
                int num = Integer.parseInt(out);
                for (int v = 1; v <= num; v++) {
                f = f * v;
                }
                String result_string = Integer.toString(f);
                T.setText(result_string);
            }
            }
            else {
            T.setText("0");
            }
        }
        else {
            System.out.println("The button with event: " + event + " has not yet been implemented");
        }
        }

        private void numberButton(String number, String out) {
        String s = T.getText();
        if (!s.isEmpty()) {
            char c[] = new char[10];
            c = s.toCharArray();
            boolean a = true;
            for (int m = 0; m < c.length; m++) {
            if (!Character.isDigit(c[m])) {
                a = false;
                break;
            }
            }
            if (!a) {
            T.setText(T.getText() + number);
            }
            else {
            int num = Integer.parseInt(out);
            String result_string = Integer.toString(num);
            T.setText(result_string + number);
            }
        }
        else {
            T.setText(number);
        }
        }
    }

@stultuske thank U sooooo much ^^
but there is a quest. -> what is the (else) statement in the line 201 for ?
and also there is something wierd accure here >> if U have time plz check it :(( is that >>when I add the numbers it doesn't give me the right ansewrs :(((
I don't know why :(
thank U again ^
^ :))))

You have buttons for which you did not have the logic in the actionPerformed.
so, the actionPerformed will be reached, but there is no code handling the functionality of that button.
Basically: earlier, if you pressed one of those buttons, you would go into the actionPerformed, but no code would be executed, since it was not of one of the buttons for which you already implemented something there. nothing would happen, and there was no way of knowing if the code did something. now, because of that else, you see that the actionperformed was executed, but that particular button doesn't have any functionality attached to it.

That's a nice refactoring,... for Java 7 ;)

Now for current Java you can take it a lot further and simplfy the code massively. I'm referring to the event handler(s). Just pass the action handling code to createButton, and completely get rid of the hidious 120 line universal ActionListener

private JButton createButton(String label, int size1, int loc1, int loc2, ActionListener al) {
   ...
   button.addActionListener(al);
   ...
}

then

// for one-line actions...
add(createButton("tansh", 70, 40, 100, 
    (e)->T.setText(Double.toString(Math.tanh(Double.parseDouble(out))))));
(etc)

// for more compilcated actions...
add(createButton("+", 265, 340, this::addMethod));
(etc)
...

void addMethod(ActionEvent e) {
    Double number = Double.parseDouble(out);
    double addition = 0;
    (etc)
}
(etc)

Edited 1 Year Ago by JamesCherrill

James: I know. But here I'm still working with Java 6, so got bored of all the nice compilation-warnings :)

@stultuske >>ok ..I got it :)))
thank U :)))
but I still don't know how to solve my pro. :((( what should I do ?!
could U help me plz ? ^_^
If U don't have time >>it's ok ,,thanks again :)

@stultuske >>finally I found the solution for my pro. >> ^__________^
Acutally I like the refactoring U did >> it makes the code more easier to work with :)
thanks ^_^

the pro. was that I could'nt add 2 numbers >>the result is always wrong >>and that's because the seconde number value is wrong !
so the solution was by writing a switch statement and other func.'s :P

This question has already been answered. Start a new discussion instead.