0

I have a problem. The user input(inputUser) must be compared with the random number. Its kinda like a gamble program. But I dont know how can I compare the input user string and the random number. And after that it is compared the output is in a dialog. The input of the user comes with a string and the random number comes with a int. I already tried to convert the int to the string. but for some reason it doesnt work.

package gamble;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import java.util.Random;
import java.util.Scanner;

public class Gamble extends JFrame {

public JLabel inputUser;
public JPanel panel;

Font myFont = new Font("Serif", Font.BOLD, 25);
Font rulesFont = new Font("Serif", Font.BOLD, 15);

 public static void main(String[] args) {
   Gamble GUI = new Gamble();
   GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   GUI.setSize(600, 600);
   GUI.setResizable(false);
   GUI.setVisible(true);
   GUI.setLocationRelativeTo(null);
}    

public Gamble(){
    super("NUMERO");

   JPanel panel = new JPanel();
   panel.setLayout(null);
   add(panel); 

   //nieuwe label
   JLabel label = new JLabel("Raad het getal");
   label.setLayout(null);
   label.setBounds(250,10, 300, 30);
   label.setFont(myFont);
   panel.add(label);

   //nieuwe label
   JLabel rules = new JLabel("Gok een nummer tot en met 5"); 
   rules.setBounds(225,40,300,30);
   rules.setFont(rulesFont);
   panel.add(rules);

   //nieuw textfield
   JTextField inputUser = new JTextField(100);
   inputUser.setBounds(275,100,100,30);
   inputUser.setFont(rulesFont);
   inputUser.setBackground(Color.LIGHT_GRAY );
   panel.add(inputUser);

   thehandler handler = new thehandler();
   inputUser.addActionListener(handler);
}

private class thehandler implements ActionListener {

    public void actionPerformed(ActionEvent event){

        Random rand = new Random(); //random number 
        int n = rand.nextInt(5) + 1; //random number wordt gemaakt

        int j = Integer.parseInt(inputUser.getText());

        if (event.getSource()== inputUser){ 
            if(n == j){
            JOptionPane.showMessageDialog(null, "test");
            }
        }
        else {
            JOptionPane.showMessageDialog(null, "dfd"); 
        }  
    }  
}

}

1
  • Did you try to print both numbers to the console, and see if they are equal? And does the else block get executed or does the code not reach that point either? Commented Dec 3, 2018 at 12:02

1 Answer 1

1

There're some problems with the declaration of inputUser, just change the global declaration of it to JTextField and remove the local declaration of it.

The code should looks like below:

class Gamble extends JFrame {

public JTextField inputUser;
public JPanel panel;

Font myFont = new Font("Serif", Font.BOLD, 25);
Font rulesFont = new Font("Serif", Font.BOLD, 15);

public static void main(String[] args) {
    Gamble GUI = new Gamble();
    GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GUI.setSize(600, 600);
    GUI.setResizable(false);
    GUI.setVisible(true);
    GUI.setLocationRelativeTo(null);
}

public Gamble(){
    super("NUMERO");

    JPanel panel = new JPanel();
    panel.setLayout(null);
    add(panel);

    //nieuwe label
    JLabel label = new JLabel("Raad het getal");
    label.setLayout(null);
    label.setBounds(250,10, 300, 30);
    label.setFont(myFont);
    panel.add(label);

    //nieuwe label
    JLabel rules = new JLabel("Gok een nummer tot en met 5");
    rules.setBounds(225,40,300,30);
    rules.setFont(rulesFont);
    panel.add(rules);

    //nieuw textfield
    inputUser = new JTextField(100);
    inputUser.setBounds(275,100,100,30);
    inputUser.setFont(rulesFont);
    inputUser.setBackground(Color.LIGHT_GRAY );
    panel.add(inputUser);

    thehandler handler = new thehandler();
    inputUser.addActionListener(handler);
}

private class thehandler implements ActionListener {

    public void actionPerformed(ActionEvent event){

        Random rand = new Random(); //random number
        int n = rand.nextInt(5) + 1; //random number wordt gemaakt

        int j = Integer.parseInt(inputUser.getText());

        if (event.getSource()== inputUser){
            if(n == j){
                JOptionPane.showMessageDialog(null, "Yep, you're right");
            }
            else {
                JOptionPane.showMessageDialog(null, "Nope nope nope, the number is " + n);
            }
        }
    }
}
}
Sign up to request clarification or add additional context in comments.

2 Comments

This correct, maybe add some explanation about how shadowing variables can lead to hard to detect the null pointers like the one created here?
Well, as we all know, Java allows this thing (the instance variable and local variable to have the same name), the compiler will compile without a complaint. So it's not a syntax error but it falls into a logical error category where the inputUser was redeclared in the local scope and assigned as well so the instance variable doesn't change at all which actually lead to the null pointer exception and in this case there's one more error about the data type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.