0
gen.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            String input = key.getText();
            if(input.toLowerCase() == "c"){
                System.out.println("C major!");
            }else{
                System.out.println("Invalid!");
            }
        }
    });

Gen is a JButton while key is a JTextField Here I attempt to check the value of key but the output continues to display "Invalid!" I'm not sure if I'm missing something or if there is another approach to this.

BruceTheGoose
  • 63
  • 1
  • 10
  • 1
    This has nothing to do with JTextFields, JButtons or Swing and all to do with how to compare Strings. Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Nov 14 '15 at 03:56
  • So instead of `if(input.toLowerCase() == "c"){`, do something like `if("c".equalsIgnoreCase(input)) {` – Hovercraft Full Of Eels Nov 14 '15 at 03:57
  • Ahh I see... Much appreciated thank you! – BruceTheGoose Nov 14 '15 at 03:58

0 Answers0