Results 1 to 4 of 4
  1. #1
    toycar is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Question 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..

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default 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:

    Java 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:
    Java 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.

  3. #3
    toycar is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default 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

    Java 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

    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;
    }
    
    }
    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?
    Last edited by toycar; 03-17-2012 at 03:44 AM.

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default 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:
    Java Code:
    private enum Variables {
        NAME, SURNAME, AGE;
    }
    Then use it like this:
    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;
    }
    Note that this is case-sensitive so I used toUpperCase() to make it case-insensitive.

Similar Threads

  1. Question about Keyboard input
    By littlebirdpoo in forum New To Java
    Replies: 3
    Last Post: 12-17-2011, 04:33 PM
  2. Read input from keyboard
    By bison in forum New To Java
    Replies: 2
    Last Post: 11-20-2010, 06:48 PM
  3. Keyboard Input In A Seperate Class?
    By FatalSylence in forum New To Java
    Replies: 6
    Last Post: 10-12-2010, 05:29 AM
  4. How to use another image using a keyboard input
    By Rekuta in forum New To Java
    Replies: 0
    Last Post: 05-13-2010, 05:00 PM
  5. Basic keyboard input and edit
    By BHCluster in forum New To Java
    Replies: 18
    Last Post: 08-08-2008, 11:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •