Accessing a keyboard input element name in a Class?
Hi, I am very fresh to JAVA, here is my question: I have a combobox with 5 inputs. My aim is to search in an array[10] of a class which has elements like string name, string surname, int age, string country, int number. And user will select which of these to search in and since the item names differ from the class element names, I got to convert them like the item in the combobox is Place of Birth but the element in the class is String CoB, so I did:
Object obj = combobox.getSelectedItem();
String TyP="";
if(obj=="Place of Birth") TyP="CoB";
else if .......
and when I try to access the value over "TyP" instead of personArray[i].name or .lastname or .age since user defines it (TyP can be .name or .lastname or .age etc.)
if(personArray[i].TyP == keyword) ....
This doesn't work.. What is the correct spelling of it? Or do I have any other mistake?
Thx in advance for ur replies..
Re: Accessing a keyboard input element name in a Class?
If you are storing an array of String in your combobox, you should convert the Object back to String or whatever objects you are using.
Example:
Code:
Object obj = combobox.getSelectedItem();
String str = (String) obj;
Now that you have a String, you can compare the *value* of the String to something else:
Code:
if (str.equals("Place of Birth")) {
//we have a match
} else {
//handle string not found
}
Using '==' with two Strings is undesired, because you are usually looking for the value of the String (i.e. the text), where as '==' will only compare if both Strings reference the same object.
Re: Accessing a keyboard input element name in a Class?
Thanks ozzyman, I will change it, but what about using a variable as an array element name?
Let me simplify;
personArray[0].name="josh";
"name" is an element and this is okay but what about this
String variable="name";
personArray[0].variable="josh";
This is inaccurate.. How can I write it then?
Edit: This is the code I am working on
Code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Object obj = type_t.getSelectedItem();
String turu="";
String obje=(String) obj;
if(obje.equals("Student No")) turu="Std_No";
else if(obje.equals("Name")) turu="F_Name";
else if(obje.equals("Surname")) turu="L_Name";
else if(obje.equals("Age")) turu="Age";
else if(obje.equals("Sex")) turu="Sex";
else if(obje.equals("Birth Place")) turu="CoB";
String keyword=(String) find_std_t.getText();
int n=0;
for (; n < personArray.length; n++)
{
if (personArray[n].Std_No==keyword)
break;
}
all_students_t.setText("");
all_students_t.append(personArray[n].Std_No+"***"+personArray[n].F_Name+"***"+personArray[n].L_Name+"***"+personArray[n].Age+"***"+personArray[n].Sex+"***"+personArray[n].CoB+"\r\n");
}
This is the class
Code:
public class Person {
public int Std_No;
public String F_Name;
public String L_Name;
public int Age;
public String Sex;
public String CoB;
public Person()
{
this.Std_No=0;
this.F_Name="";
this.L_Name="";
this.Age=0;
this.Sex="";
this.CoB="";
}
public Person(int std_no, String f_name, String l_name, int age, String sex,String cob)
{
this.Std_No=std_no;
this.F_Name=f_name;
this.L_Name=l_name;
this.Age=age;
this.Sex=sex;
this.CoB=cob;
}
}
User will search within the array by choosing in which element, then enter a keyword which can either be a string or an int, then it will return the matches in the textarea (all_students_t). How must I change this code?
Re: Accessing a keyboard input element name in a Class?
You can't do that in Java (although I think it is possible in other languages such as JavaScript).
There is a little work-around in Java though, with Enums:
First define an enum set with all of your variable names:
Code:
private enum Variables {
NAME, SURNAME, AGE;
}
Then use it like this:
Code:
String variable = "name";
Variables v = Variables.valueOf(variable.toUpperCase());
switch (v) {
case NAME:
personArray[0].name = ...;
break;
case SURNAME:
personArray[0].surname = ...;
break;
case AGE:
personArray[0].age = ...;
break;
default: break;
}
Note that this is case-sensitive so I used toUpperCase() to make it case-insensitive.