user3721668
user3721668

Reputation: 29

A simple error i can't resolve

public class Quiz {

    private static void homePage() {
        JFrame homePage = new JFrame("QUIZ");
        Container c = homePage.getContentPane();
        homePage.setVisible(true);
        homePage.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        homePage.setBounds(600,180,700,400);
        homePage.setResizable(false);
        homePage.setContentPane(c);

        JLabel benvenuto = new JLabel("Benvenuto!");
        benvenuto.setFont(new Font("Serif", Font.BOLD, 30));
        benvenuto.setBounds(265,0,180,120);
        benvenuto.setOpaque(true);

        JLabel label1 = new JLabel("Inserisci il tuo nome:");
        label1.setBounds(215,100,120,100);
        label1.setOpaque(true);

        JTextArea areaNome = new JTextArea();
        areaNome.setEditable(true);
        areaNome.setFont(new Font("Serif", Font.BOLD, 16));
        areaNome.setBounds(345,142,120,20);
        javax.swing.border.Border borderP = BorderFactory.createLineBorder(Color.BLACK);
        areaNome.setBorder(borderP);

        JLabel labelStarting = new JLabel();
        labelStarting.setBounds(230,230,280,120);
        labelStarting.setForeground(Color.RED);

        JButton startButton = new JButton("Inizia");
        startButton.setBounds(280,200,120,50);
        startButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                String gt1 = areaNome.getText();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Quiz.class.getName()).log(Level.SEVERE, null, ex);
                }
                labelStarting.setText("Preparati "+gt1+", il gioco sta per iniziare.");

                JFrame quizPage = new JFrame("QUIZ");
                quizPage.setBounds(573,150,750,450);
                quizPage.setVisible(true);
                quizPage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                quizPage.setResizable(false);
                Container c2 = quizPage.getContentPane();
                quizPage.setContentPane(c2);

                JLabel domanda1 = new JLabel("1) Qual è la capitale d'Italia?");
                domanda1.setBounds(0,0,280,120);

                JLabel domanda2 = new JLabel("2) Quanto fa 2^8?");
                domanda2.setBounds(0,55,280,120);   

                JLabel domanda3 = new JLabel("3) Quando è stata scoperta l'America da Colombo?");
                domanda3.setBounds(0,110,380,120);

                JLabel domanda4 = new JLabel("4) Dove si trova l'Empire State Building?");
                domanda4.setBounds(0,165,280,120);

                JLabel domanda5 = new JLabel("5) Qual è il nome dell'autore della Divina Commedia?");
                domanda5.setBounds(0,220,380,120);

                JLabel labelNull2 = new JLabel();
                labelNull2.setBounds(230,230,280,120);

                JTextArea r1 = new JTextArea();
                r1.setBounds(320,50,120,20);
                javax.swing.border.Border border1 = BorderFactory.createLineBorder(Color.BLACK);
                r1.setBorder(border1);

                JTextArea r2 = new JTextArea();
                r2.setBounds(320,105,120,20);
                javax.swing.border.Border border2 = BorderFactory.createLineBorder(Color.BLACK);
                r2.setBorder(border2);

                JTextArea r3 = new JTextArea();
                r3.setBounds(320,160,120,20);
                javax.swing.border.Border border3 = BorderFactory.createLineBorder(Color.BLACK);
                r3.setBorder(border3);

                JTextArea r4 = new JTextArea();
                r4.setBounds(320,217,120,20);
                javax.swing.border.Border border4 = BorderFactory.createLineBorder(Color.BLACK);
                r4.setBorder(border4);

                JTextArea r5 = new JTextArea();
                r5.setBounds(320,270,120,20);
                javax.swing.border.Border border5 = BorderFactory.createLineBorder(Color.BLACK);
                r5.setBorder(border5);

                JButton br1 = new JButton("Conferma");
                br1.setBounds(475,49,120,25);

                JButton br2 = new JButton("Conferma");
                br2.setBounds(475,105,120,25);

                JButton br3 = new JButton("Conferma");
                br3.setBounds(475,160,120,25);

                JButton br4 = new JButton("Conferma");
                br4.setBounds(475,216,120,25);

                JButton br5 = new JButton("Conferma");
                br5.setBounds(475,270,120,25);

                JLabel lp1 = new JLabel();
                lp1.setBounds(635,45,100,30);
                lp1.setForeground(Color.GREEN);

                JLabel lp2 = new JLabel();
                lp2.setBounds(635,103,100,30);
                lp2.setForeground(Color.GREEN);

                JLabel lp3 = new JLabel();
                lp3.setBounds(635,158,100,30);
                lp3.setForeground(Color.GREEN);

                JLabel lp4 = new JLabel();
                lp4.setBounds(635,213,100,30);
                lp4.setForeground(Color.GREEN);

                JLabel lp5 = new JLabel();
                lp5.setBounds(635,269,100,30);
                lp5.setForeground(Color.GREEN);

                JLabel lp11 = new JLabel();
                lp11.setBounds(635,45,100,30);
                lp11.setForeground(Color.RED);

                JLabel lp22 = new JLabel();
                lp22.setBounds(635,103,100,30);
                lp22.setForeground(Color.RED);

                JLabel lp33 = new JLabel();
                lp33.setBounds(635,158,100,30);
                lp33.setForeground(Color.RED);

                JLabel lp44 = new JLabel();
                lp44.setBounds(635,213,100,30);
                lp44.setForeground(Color.RED);

                JLabel lp55 = new JLabel();
                lp55.setBounds(635,269,100,30);
                lp55.setForeground(Color.RED);

                br1.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e)
                    {
                        String sr1 = r1.getText();
                        if(sr1=="Roma") {
                            lp1.setText("ESATTO");
                        }else {
                            lp11.setText("ERRATO");
                        }
                    }
                });

                br2.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e)
                    {
                        String sr2 = r2.getText();
                        if(sr2=="256") {
                            lp2.setText("ESATTO");
                        }else {
                            lp22.setText("ERRATO");
                        }
                    }
                });

                br3.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e)
                    {
                        String sr3 = r3.getText();
                        if(sr3=="1492") {
                            lp3.setText("ESATTO");
                        }else {
                            lp33.setText("ERRATO");
                        }
                    }
                });

                br4.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e)
                    {
                        String sr4 = r4.getText();
                        if(sr4=="New York"||sr4=="new york") {
                            lp4.setText("ESATTO");
                        }else {
                            lp44.setText("ERRATO");
                        }
                    }
                });

                br5.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e)
                    {
                        String sr5 = r5.getText();
                        if(sr5=="Dante"||sr5=="dante") {
                            lp5.setText("ESATTO");
                        }else {
                            lp55.setText("ERRATO");
                        }
                    }
                });


                c2.add(domanda1);
                c2.add(domanda2);
                c2.add(domanda3);
                c2.add(domanda4);
                c2.add(domanda5);
                c2.add(r1);
                c2.add(r2);
                c2.add(r3);
                c2.add(r4);
                c2.add(r5);
                c2.add(br1);
                c2.add(br2);
                c2.add(br3);
                c2.add(br4);
                c2.add(br5);
                c2.add(lp1);
                c2.add(lp2);
                c2.add(lp3);
                c2.add(lp4);
                c2.add(lp5);
                c2.add(lp11);
                c2.add(lp22);
                c2.add(lp33);
                c2.add(lp44);
                c2.add(lp55);
                c2.add(labelNull2);
            }
        });



        JLabel labelNull = new JLabel();



        c.add(benvenuto);
        c.add(label1);
        c.add(areaNome);
        c.add(startButton);
        c.add(labelStarting);
        c.add(labelNull);

    }

    public static void main(String[] args) {
        homePage();
    }
}

When I run my application I get an error in the Action Event: in the if question don't return ESATTO but return ERRATO...

Upvotes: 0

Views: 184

Answers (2)

Mustafa sabir
Mustafa sabir

Reputation: 4360

Your question seems a bit confusing, When I run my application I get an error in the Action Event: in the if question don't return ESATTO but return ERRATO. Is it that you're getting some runtime error in your code or you're stating the wrong return type as an error. Also your code is too long, always post the part of code that is producing errors.

For your return part of if condition, you should not compare strings using == operator. The reason is that whenever we create new strings EX:- String s ="hello" ,java adds the string literals into a string pool. so that next time when I create String n= "hello" It will not create a new string object, but assign n to point to hello from the string pool itself. Hence if I do this:-

String h="hello";
String n="hello";
if(n==h){//return true because they are pointing to the same string object in the pool};

But if I do this :-

String h="hello";
String n=new String("hello");//explicitly creating a new string object although there is one already in the pool
if(n==h){//return false because they are now poiting to the different string object in the pool};

See that although s and n are equal in values they are not equal in terms of references, and == is meant for comparing references in objects(it works fine in case of primitives like int, float etc, but Strings are objects).

To actually compare values of Strings (Which is usually the case), you must always use .equals() method. This compares the string in terms of its content only, no relation to references whatsoever.

So change you code according to this, and it should work( if assuming the error is only in logic). An example snippet from your code would change from this

String sr5 = r5.getText();
if(sr5=="Dante"||sr5=="dante") {
lp5.setText("ESATTO");
}else {
lp55.setText("ERRATO");
}

to this:-

String sr5 = r5.getText();
if(sr5.equals("Dante")||sr5.equals("dante")) {
lp5.setText("ESATTO");
}else {
lp55.setText("ERRATO");
}

Upvotes: 1

Alexandre Lavoie
Alexandre Lavoie

Reputation: 8771

You can't compare String's by using ==, it will only compare the reference, which will always be false.

Use sr1.equals("Roma") instead.

Read more

Upvotes: 3

Related Questions