Sending text read from GUI to a method

Discussion in 'Java' started by gaff, Sep 12, 2006.

  1. gaff

    gaff Guest

    Hey,

    Have a problem sending a String from a GUI to a method.

    The method definitely works as I can use with text that I set in code.

    track.deleteFromList("some object name"); // removes object called
    "some object name" from the list that the track object stores.

    When I try to use a text box to read in the text it doesn't work.

    The String is read in correctly, checked.

    The String is read by method correctly, checked.

    It should then make a comparison with each place in the list to see if
    it's the correct identifier (Strnig is identifier/name of the object to
    be deleted) but doesn't seem to realise when there is a match and
    therefore should execute remove form list.

    I suspect that the String has to be declared in some special way before
    it is passed to the method? as it is not of definite value when the
    prog starts running. It's read and fed properly, but because it's
    dynamic it's not being compared correctly?

    Relevant code is below, thanks very much for your help.

    t1: text box, b1: button


    FROM MAIN, everything else works, track is instance of Tracker2 class
    ///////////////////////////////////////////////////////////////////////
    b2.addSelectionListener(new SelectionAdapter(){
    public void widgetSelected(SelectionEvent e) {

    String inny = t1.getText();

    track.deleteFromList(inny);

    }
    });
    ///////////////////////////////////////////////////////////////////////


    import java.util.*;


    public class Tracker2 {

    private List<Array_test> list_of_test_objects;

    //construct
    public Tracker2(){
    list_of_test_objects = new ArrayList<Array_test>();
    }
    ........
    .......
    .......
    .......


    // delete from list given a certain identifier
    public void deleteFromList(String old_array_test_object) {
    int i = 0;
    while (i < list_of_test_objects.size()) {
    if ((list_of_test_objects.get(i).getIdentifier()) ==
    old_array_test_object) {
    list_of_test_objects.remove(i);
    i = list_of_test_objects.size();
    }
    i++;
    }
    }


    .......
    .......
    .......
    .......
     
    gaff, Sep 12, 2006
    #1
    1. Advertising

  2. gaff wrote:
    > Hey,
    >
    > Have a problem sending a String from a GUI to a method.
    >
    > The method definitely works as I can use with text that I set in code.
    >
    > track.deleteFromList("some object name"); // removes object called
    > "some object name" from the list that the track object stores.
    >
    > When I try to use a text box to read in the text it doesn't work.
    >

    [snip]
    >
    > // delete from list given a certain identifier
    > public void deleteFromList(String old_array_test_object) {
    > int i = 0;
    > while (i < list_of_test_objects.size()) {
    > if ((list_of_test_objects.get(i).getIdentifier()) ==
    > old_array_test_object) {


    Never use == to compare two strings. Use the equals method.

    The expression

    (list_of_test_objects.get(i).getIdentifier()).equals(old_array_test_object)

    should work

    BK
     
    Babu Kalakrishnan, Sep 12, 2006
    #2
    1. Advertising

  3. gaff

    gaff Guest

    Cheers buddy, that was it.
     
    gaff, Sep 12, 2006
    #3
  4. gaff

    Lew Guest

    gaff wrote:
    > int i = 0;
    > while (i < list_of_test_objects.size()) {
    > if ((list_of_test_objects.get(i).getIdentifier()) ==
    > old_array_test_object) {
    > list_of_test_objects.remove(i);
    > i = list_of_test_objects.size();
    > }
    > i++;
    > }


    Just a suggestion: you might find ending the loop with
    break;
    instead of
    i = list_of_test_objects.size();

    marginally more efficient than the call to size() and assignment to i followed
    by the increment and test of i.

    Speaking strictly for myself, I prefer the idiom

    for( int i=0; i < loto.size(); i++) {...}

    preferable to

    int i=0; while( i < loto.size() ) { ...; i++ }

    for compactness and because it limits the scope if i.

    Actually, I prefer

    for ( X someX : listOfX ) { ... } // X is base type of list

    to either of the above, because it avoids explicit declaration of all
    loop-control variables, and it is just so purty.

    -Lew
     
    Lew, Sep 14, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,440
  2. Simon Rigby
    Replies:
    5
    Views:
    761
    Cowboy \(Gregory A. Beamer\)
    Oct 7, 2006
  3. Luke
    Replies:
    2
    Views:
    4,780
    Nigel Wade
    Mar 15, 2007
  4. Ron Coutts
    Replies:
    0
    Views:
    129
    Ron Coutts
    Nov 11, 2003
  5. Trans
    Replies:
    3
    Views:
    177
    Trans
    Dec 21, 2005
Loading...

Share This Page