Reputation: 178
I am storing the data of a small form of a JFrame in a array. I compare the number that was entered in the form with the number that the user is looking for. But it shows only shows the result of the last student added, those that already add does not find them
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
if(counter< studentList.length){
String code= txtcode.getText();
String name= txtname.getText();
String lastname= txtlastname.getText();
String rh = comborh.getSelectedItem().toString();
int age= Integer.parseInt(txtage.getText());
//the object is created
Student objStudent = new Student();
objStudent.setCode(code);
objStudent.setName(name);
objStudent.setLastname(lastname);
objStudent.setAge(age);
objStudent.setRH(rh);
studentList[counter] = objStudent;
counter++;
JOptionPane.showMessageDialog(this,"n° "+ counter+ " Students.");
}else{
JOptionPane.showMessageDialog(this,"Error","Error", JOptionPane.ERROR_MESSAGE);
}
}
the information is added without problem in the array
this is the condition
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String code= txtcode.getText();
String name= txtname.getText();
String search= JOptionPane.showInputDialog(this,"search by code");
for (int i = 0; i<=studentList.length;i++){
if ((search).equals(code)){
JOptionPane.showMessageDialog(this,"code with the name of: "+name);
}
}
}
Upvotes: 0
Views: 320
Reputation: 89
In the method jButton1ActionPerformed
you want to check whether the content of the two string-variables search
and code
are the same and not whether their places in memory are the same.
Remember that Java works with variables that hold references to memory if that variable holds a reference object (and an instance of the class String
is a reference object). These kind of variables are called pointers in languages like C, the content of a pointer is a memory address. In Java, reference variables don't work the same for the programmer as pointers do in C but behind the scenes, they are exactly the same. Almost all variables in Java are holding references to memory, only primitive types don't (examples of primitive types: char, int, double, float, etc.).
Why is this important you may ask? Well, if you use the == operator on reference objects, you're not checking whether the content of two strings are the same, but you're checking whether the reference to both objects is the same. And these are fundamentally different comparisons.
If you wan't to check whether two strings have the same content, you need to use the equals(String)
method which is member of the class String and which compares the content of two String-objects (and that's exactly what we want).
Now, knowing all of this, it should be no surprise that the line
if (search == code) {
should be replaced with
if (search.equals(code)) { // which is equivalent to if (code.equals(search)) {
If you want to know more about pointers (references to memory) I really encourage you to do so by looking into it, for instance by reading some documentation about the programming language C.
You may also wan't to read more about the equals-method of the class Object which is overriden in the class String.
Upvotes: 1