Hi, I’m relatively new to java, and brand new to these forums. In a simple program i’m writing just to test out my skills(what little I have). It’s a simple chat program in which the program asks how you are doing, the user types something in the field, and depending on the answer(good great etc.) it will have a reply. The problem i’m having is that I can’t get the program to respond to the input. I know that the listeners and stuff are correct because I used System.out.println() where the argument is the text in the field, and that works fine. If you want, I can post the source code. This probably has a very simple answer, but for now I’m stumped. :-/
,Leppy
better than saying to post the code snippet -->
posting the code snippet !
Huh? ???
I’ll post the code as soon as I can. Thank you for answering!
*edit: here’s the code(It’s in the noob section for a reason ;))
import javax.swing.;
import java.awt.event.;
import java.awt.*;
public class Chat extends JFrame implements ActionListener {
String questiont = “How are you doing today?”;
JTextField response = new JTextField(40);
JLabel question = new JLabel(questiont);
JButton send = new JButton (“Send!”);
public Chat() {
super();
setSize(350, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
response.addActionListener(this);
send.addActionListener(this);
Container pane = getContentPane();
BorderLayout bord = new BorderLayout();
pane.add(question, BorderLayout.NORTH);
pane.add(response, BorderLayout.CENTER);
setContentPane(pane);
setVisible(true);
}
public void actionPerformed(ActionEvent input) {
String evt = input.getActionCommand();
String answer = evt;
System.out.println(evt);
if (evt == "good") {
String questiont = "Not great?";
} else if (evt == "great") {
String questiont = "Great!";
}else if (evt == "Terrible") {
String questiont = "That's too bad";
} else if (evt == "bad") {
String questiont = "I hope you cheer up";
} else {
String questiont = "Could you repeat that? I couldn't understand";
}
}
public static void main(String[] arguments) {
Chat chat1 = new Chat();
}
}
Instead of setting questiont in the actionlistener method, call question.setText(“blah”); [not sure if it’s setText, setLabel, or whatever]. Strings are immutable.
hmmm, now it just goes to the doesn’t understand line ???
here’s what I changed(it’s setText() BTW)
[quote]public void actionPerformed(ActionEvent input) {
String evt = input.getActionCommand();
String answer = evt;
System.out.println(evt);
if (evt == “good”) {
question.setText(“I’m glad to hear that.”);
} else if (evt == “great”) {
question.setText(“Great!”);
}else if (evt == “Terrible”) {
question.setText(“That’s too bad”);
} else if (evt == “bad”) {
question.setText(“I hope you cheer up”);
} else {
question.setText("Could you repeat that? I couldn't understand");
}
}
[/quote]
Instead of (evt == “Good”), try (“Good”.equals(evt)). (The main point is to use the equals() method. I’ve just switched the order to follow a common idiom that makes it impossible to get a NullPointerException.)
Willie
Thanks for the help! It works now. i had a lot of projects on hold because of this thank you. ;D
In case anyone else has a similar problem, this is what went wrong:
The == operator tests reference equality, while the equals() method usually tests object equality. Two objects may to all intents and purposes be the same, but if they are actually different objects, then the references will not be the same, so the == test will return false.
However, if you perform the obvious test to confirm this, you’ll get a different result!
public static void main(String[] args){
String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1 == s2);
}
This will actually output “true”.
There are two things that lead to this non-obvious result:
[]Strings are immutable
As there is no difference between the two “Hello” objects, the compiler is allowed to create a single instance and point both references at it.
[]String.intern()
The String class maintains a pool of String objects, and all String literals are placed into it. Even if the compiler didn’t share the instances, both references will be pointing at the same object anyway.
In summary, use equals() when you want to check whether two objects represent the same information; use == when you want to check whether two references are pointing at the same object.
[quote][…]use == when you want to check whether two references are pointing at the same object.
[/quote]
…which you probably never want to do. IMHO, even if you are sure that two Strings are pointing to the same object, it’s safer to use .equals()
that’s a great summary! I will definiatly remember that. I am very grateful for you help!