Cameron Soto
Cameron Soto

Reputation: 3

showInputDialog displays too many times after selecting no

I want Play() to run again if String confirm input is anything other than "Y" or "y". When I press the war button, it shows the input dialog, and if I input "N" it will function as intended. However, if I press any button and input "N" again in the same program run, it will asks for confirmation a 2 times. If I do it again, it asks for confirmation 4 times, and it continues this pattern by powers of 2. What is causing that, and is there a better way to confirm the users choice?

I've tried setting String confirm equal to " " in the else{} just before it runs play() again, which didn't work and I didn't expect to work anyway. But other than that I have no idea.

public class Main {

public static void main(String[] args) {
    GUI heroSelect = new GUI(450, 200, "Inscription", new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    JLabel heroLabel = new JLabel("Choose your class, hero.");
    heroLabel.setFont(new Font("Serif", Font.PLAIN, 22));
    c.gridx = 1;
    c.gridy = 0;
    c.weightx = .5;
    c.weighty = .5;
    heroSelect.add(heroLabel, c);

    JButton war = new JButton("Warrior");
    c.gridx = 0;
    c.gridy = 2;
    heroSelect.add(war, c);

    JButton mage = new JButton("Mage");
    c.gridx = 1;
    c.gridy = 2;
    heroSelect.add(mage, c);

    JButton rog = new JButton("Rogue");
    c.gridx = 2;
    c.gridy = 2;
    heroSelect.add(rog, c);

    play(war, mage, rog);

}

public static void play(JButton war, JButton mage, JButton rog) {
    war.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String confirm = JOptionPane.showInputDialog(null, "The warrior "
                    + "is a strong, hard-hitting class. It relies on raw "
                    + "damage and heals through offensive abilities.\n "
                    + "However, the warior does not possess any direct "
                    + "healing or spells. Are you sure you want to choose "
                    + "this class? Y/N");
            if(confirm.equalsIgnoreCase("Y")) {
                //TBD
            }
            else {
                play(war, mage, rog);
            }
        }

    });

    mage.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String confirm = JOptionPane.showInputDialog(null, "");//ADD DESCRIP
            if(confirm.equalsIgnoreCase("Y")) {

            }
            else {
                play(war, mage, rog);
            }
        }

    });

    rog.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String confirm = JOptionPane.showInputDialog(null, "");//ADD DESCRIP
            if(confirm.equalsIgnoreCase("Y")) {

            }
            else {
                play(war, mage, rog);
            }
        }

    });
}

}

showInputDialog should close when the user enters "N" or "n", and should re-run play() to allow the user to look at the description of other classes and eventually choose one. Instead, inputting "N" or anything other than "Y" or "y" for that matter results in multiple showInputDialog prompts back to back.

Upvotes: 0

Views: 44

Answers (2)

Mark Mc Adam
Mark Mc Adam

Reputation: 46

What you are doing by calling the play() method inside of itself is called recursion, every time you call it you are adding more EventListeners to the buttons.

I think that a do-while loop that repeatedly checks the input, in place of the if-else will work for you.

do {
    String confirm = ...
    ....
    if(confirm.equalsIgnoreCase("N")) {
        break;
    }
} while (!confirm.equalsIgnoreCase("Y"))

// Code to run game or whatever is next...

Upvotes: 2

m.s
m.s

Reputation: 39

You call Play method in listeners that are created in Play method. and everytime you make new listener for each hero. Also read about OOP (it is more important to use objects in java than using GUI for starters :))

As the question about better ways in general I really encourage You to get familiar with patterns. Just gathering inputs are really simple thing and you have multiple choices like looping switch case for example.

Upvotes: 1

Related Questions