0

I am trying to outprint a possible two different lines in to 1 text box based on the selection of a combobox.

I want to print this in to textField called textACall

here is my code.. I am getting error at the if statement.

  private void answerCallActionPerformed(java.awt.event.ActionEvent evt) {                                           
        try {
        String sql = "SELECT Answer FROM contact";
        pst = conn.prepareStatement(sql);
        rs = pst.executeQuery();

                String answer = rs.getString("answer");

                if (answer == 0) {
                    System.out.println ("Answer Call!");
                }

                else {
                    System.out.println("Do Not Answer!");
                }
    }
        catch(Exception e) {
        JOptionPane.showMessageDialog(null, e);
        }
    }    
wuno
  • 9,547
  • 19
  • 96
  • 180
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – aliteralmind Mar 20 '14 at 16:59
  • if answer.equals("0"){ ... } else { ... } guess that't the error on your if statement, Strings are compared with "equals" instead of "==" – Frakcool Mar 20 '14 at 17:03
  • ya but when I change it to = that does not help. – wuno Mar 20 '14 at 17:06
  • and when i say 0 is that referring to the value 0 in the database? – wuno Mar 20 '14 at 17:06

1 Answers1

1

Don't use == when comparing Strings. Use the equals() method instead.

Edit- Wait why are you comparing a String to an int? You either need to convert the 0 to a String (which is pretty trivial, just put it in quotes) or convert the String answer to an int using the methods in the Integer class.

You can convert your answer into an int like so:

String strAnswer = rs.getString("answer");
int intAnswer = Integer.valueOf(strAnswer);
if (intAnswer == 0) {
   System.out.println ("Answer Call!");
}
else {
   System.out.println("Do Not Answer!");
}

Or you can use String comparison, notice the equals() method instead of == and the quotation marks around the 0:

String strAnswer = rs.getString("answer");
if (strAnswer.equals("0")) {
   System.out.println ("Answer Call!");
}
else {
   System.out.println("Do Not Answer!");
}

Which approach you use depends on your context. You could also convert the answer to a boolean, but that wouldn't be much different from your if statement as it is. If you have to do a lot of these conversions, it would make sense to take a String and return a boolean whether it's 0 or 1.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • ok, the value in the database is a bool. its false 0 unless it is a business then it becomes true which is 1. im trying to say if the bool value in Answer is 0 then print Answer Call else if its true 1 Do Not Answer call – wuno Mar 20 '14 at 17:01
  • ok great thanks man. its not printing to the text field and its only printing Answer Call! regardless of the value from the DB. It just shows up in the consol and not in textACall field – wuno Mar 20 '14 at 17:12
  • Then it's time to debug your program. Are you comparing Strings or ints? If you're comparing Strings, are you using the equals() method? What is the value coming out of the database? Also, you haven't posted any code that deals with a text field. The code you posted will print to the console. – Kevin Workman Mar 20 '14 at 17:14
  • im sorry, I keep getting confused. Ok so I will look up the equals method. thank you. And I was trying to get the text to appear in the textACall field as stated above – wuno Mar 20 '14 at 17:17
  • Get it working with System.out.println() first, then worry about your text field. If you're still stuck, post an [MCVE](http://stackoverflow.com/help/mcve) demonstrating what you have, with hardcoded values instead of a call to a database. – Kevin Workman Mar 20 '14 at 17:21