Ryan Hardin
Ryan Hardin

Reputation: 81

Adding a TextField to JOptionPane

I am working on a simple program that displays "Hello" and accepts user input using JOptionPane. I wanted to read the users input and compare it to the word that is displayed. For example, the program will display "Hello" and the user would have to input a word into the text box. If they type "Hello" then "Correct" will print. If they don't type Hello then "Incorrect" will print. In order to read the users input and compare the two strings, what do I need to do?

public static void main(String[] args){

    String resp = "Hello";

    JOptionPane.showInputDialog(null, resp);

    String input = ; //what should go here                              

    if (resp.compareTo(input) == 0) {
        JOptionPane.showMessageDialog(null, "Correct!");
        } else
        JOptionPane.showMessageDialog(null, "Incorrect");
        }
    }
}

Upvotes: 1

Views: 4403

Answers (1)

3kings
3kings

Reputation: 838

public static void main(String[] args)
{

String resp = "Hello";

String input = JOptionPane.showInputDialog(null, resp);                         

if (resp.compareTo(input) == 0)
    JOptionPane.showMessageDialog(null, "Correct!");
else
    JOptionPane.showMessageDialog(null, "Incorrect");
}

Upvotes: 6

Related Questions