Results 1 to 4 of 4
- 03-17-2012, 12:39 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
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:
Now that you have a String, you can compare the *value* of the String to something else:Java Code:Object obj = combobox.getSelectedItem(); String str = (String) obj;
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.Java Code:if (str.equals("Place of Birth")) { //we have a match } else { //handle string not found }
- 03-17-2012, 03:19 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
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
This is the classJava 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"); }
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?Java 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; } }Last edited by toycar; 03-17-2012 at 03:44 AM.
-
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:
Then use it like this:Java Code:private enum Variables { NAME, SURNAME, AGE; }
Note that this is case-sensitive so I used toUpperCase() to make it case-insensitive.Java 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; }
Similar Threads
-
Question about Keyboard input
By littlebirdpoo in forum New To JavaReplies: 3Last Post: 12-17-2011, 04:33 PM -
Read input from keyboard
By bison in forum New To JavaReplies: 2Last Post: 11-20-2010, 06:48 PM -
Keyboard Input In A Seperate Class?
By FatalSylence in forum New To JavaReplies: 6Last Post: 10-12-2010, 05:29 AM -
How to use another image using a keyboard input
By Rekuta in forum New To JavaReplies: 0Last Post: 05-13-2010, 05:00 PM -
Basic keyboard input and edit
By BHCluster in forum New To JavaReplies: 18Last Post: 08-08-2008, 11:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks