Search code examples
javaswingselectgettext

How to make a selective gettext()


I am trying to do a currency exchange program, but if second input(s1 here) is empty, the program gives NumberFormatException:Empty String error. It works when first(s here) is empty. So i wonder if there is an alternative way to make it selective with a one button. And why isnt it working when second field is empty ?

public class kanvas1 implements ActionListener,WindowListener{


    private JTextField tf;
    private JTextField tf1;
    private JTextField tf2;
    private JTextField tf3;
    private JLabel lb;
    private JLabel lb1;

    private JButton bt;
    private JButton bt1;

    public kanvas1()
    {

        tf=new JTextField();
        tf1=new JTextField();
        tf2=new JTextField();
        tf3=new JTextField();
        lb=new JLabel("$");
        lb1=new JLabel("TL");
        bt=new JButton("Cevir");
        bt1=new JButton("Sıfırla");


        pencere();


    }

    public void pencere() {
        tf.setBounds(50,20,150,50);
        tf1.setBounds(50,80, 150, 50);
        tf2.setBounds(220,20,150,50);
        tf3.setBounds(220,80,150,50);
        lb.setBounds(30,20,20, 50);
        lb1.setBounds(30,80,20,50);
        bt.setBounds(400,20,100, 50);
        bt1.setBounds(400,80,100,50);
        bt.addActionListener(this);
        bt1.addActionListener(this);
        JFrame ab=new JFrame();
        ab.setVisible(true);
        ab.setSize(600,200);
        ab.setLayout(null);
        ab.add(tf);ab.add(tf1);ab.add(tf2);ab.add(tf3);ab.add(bt);ab.add(bt1);ab.add(lb);ab.add(lb1);
        bt.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {


                String s=tf.getText(); //problem is here


                double a=Double.parseDouble(s);

                double c=a*5.44;

                String result=String.valueOf(c);

                tf2.setText(result);




            }

        });
        bt.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {



                String s1=tf1.getText();
                                          //and here

                double b=Double.parseDouble(s1);

                double d=b*0.18;

                String result1=String.valueOf(d);

                tf3.setText(result1);


            }

        });
        bt1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                tf.setText("");
                tf1.setText("");
                tf2.setText("");
                tf3.setText("");

            }
        });


    }


    @Override
    public void windowActivated(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowClosed(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowClosing(WindowEvent arg0) {
        System.exit(0);

    }

    @Override
    public void windowDeactivated(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowDeiconified(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowIconified(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowOpened(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    public static void main(String args[]) {

        new kanvas1();



    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }   
}

Solution

  • you can add a check before parsing like this:

    if (s1.isEmpty())
         return;
    

    It's failing because Double.parseDouble(String arg) tries to convert the string passed as an argument to a Double that the string represents. which means strings like "12", "12.0", etc. are valid inputs while Strings like "12a", "ac", "", etc. are bound to fail; because, after all, 12a is not a double(its not even a number!).

    If a text Field is empty and you execute getText() what you will retrieve is "" (an Empty String) and not null.

    so, if you want to make any conditional decision, base your condition on the text retrieved being empty i.e. "" and not null