mercredi 26 février 2020

my if statment not working i cant find out why? " if (y == "0") " [duplicate]

this is my code and my method suppose to return true, i try to use jtextarea to get 2 lines . " if (y == "0") " looks fine to me i event try charat and its not working.

any one knows how to fix it ? and thank you.


    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JTextPane txt = new JTextPane();
        txt.setEditable(false);
        txt.setText("\r\n0");
        txt.setBounds(10, 11, 414, 57);
        frame.getContentPane().add(txt);

        JButton btn = new JButton("New button");
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                txt.setText(str(txt.getText()));
            }
        });
        btn.setBounds(161, 114, 89, 23);
        frame.getContentPane().add(btn);
    }
    public String str(String o){

        String[] nt=o.split("\n");
        String x=nt[0],y=nt[1];


            if (y == "0")
            {   
            o="true";

            }
            else 
            {
                System.out.println(y);
                o="false"+y+"*";
        }
        return o;
        }

Aucun commentaire:

Enregistrer un commentaire