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++;
}
}
......
......
......
......
Babu Kalakrishnan - 12 Sep 2006 18:59 GMT
> Hey,
>
[quoted text clipped - 6 lines]
>
> 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
gaff - 12 Sep 2006 21:35 GMT
Cheers buddy, that was it.
Lew - 14 Sep 2006 13:03 GMT
> int i = 0;
> while (i < list_of_test_objects.size()) {
[quoted text clipped - 5 lines]
> 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