Sending text read from GUI to a method

G

gaff

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++;
}
}


.......
.......
.......
.......
 
B

Babu Kalakrishnan

gaff said:
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
 
L

Lew

gaff said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top