Results 1 to 5 of 5
  1. #1
    langaro is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default comparison of two (identical) strings doesn't work

    hey,
    I'm having a problem with a little program i'm writing. it's a notepad, I don't like the one on the mobile so I make my own one. I haven't done any java before but I did cpp which is very similar so I don't have any problems with the structure and the programming. learned the basic differences from some tutorials.
    my program is nearly finishes but I#m having a problem with the opening of a note.
    a new note is saved in a RecordStore with the name of the category to which it belongs - the name of the category is saved in the variable Title. so i first open the record store. then I read from the recordstore which works fine, right now I have all the records shown on screen to see if it works - it does. then I compare the current record to the variable S_Note which contains the record I'm looking for.
    In my program I see that:
    (I'm afraid I can't post any links because I haven't posted 20posts yet, but here's the link to the picture)
    flickr.com/photos/42983190@N02/3962800100/

    Obviously 'Note 1' and Note 1 in the first two textfields are the same! but when I use the if(S_Note == dis.readUTF()) I never see the textfield which says 'correct' on the screen --> my program never shows the note so there must be something wrong with this comparison!?

    here's the full code:

    Java Code:
            private void test(){
                // general variables
                form = new Form("Edit Note:");
    
                int reID = 0;
    
                //open RecordStore
                try{
                    RS = RecordStore.openRecordStore(Title, false);
                }catch(RecordStoreException rse){
                    // do something - interrupt program
                    rse.printStackTrace();
                    return;
                }
                
                //read from RecordStore
                try{
                    RecordEnumeration recenum = RS.enumerateRecords(null, null, false); // the RecordStore is called RS
                    while(recenum.hasNextElement()){
                        reID = recenum.nextRecordId();
                        ByteArrayInputStream bais = new ByteArrayInputStream(RS.getRecord(reID));
                        DataInputStream dis = new DataInputStream(bais);
                        try{
                            String zwa = "RecordNumber: " + Integer.toString(reID) + ", Title: '" + dis.readUTF() + "'";
                            txtTitle = new TextField("",zwa,500,TextField.ANY);
                            form.append(txtTitle);
                            txtTitle = new TextField("",S_Note,500,TextField.ANY);
                            form.append(txtTitle);
                            if(S_Note == dis.readUTF()){ // I know you should do it with the String.compare method but I tried that and it didn't make a difference
                                txtTitle = new TextField("","that's it",500,TextField.ANY);
                                form.append(txtTitle);
                            }
                        }catch(IOException rse){
                            rse.printStackTrace();
                        }
                    }
                }catch(RecordStoreNotOpenException rse){
                    rse.printStackTrace();
                }catch(RecordStoreException rse){
                    rse.printStackTrace();
                }
    
                // close Record Store
                try{
                    RS.closeRecordStore();
                }catch(RecordStoreException rse){
                    // do something - interrupt program
                    rse.printStackTrace();
                    return;
                }
            }
    can you please tell me what I'm doing wrong!?

    thanks a lot for your help

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Use the equals method to compare strings.

  3. #3
    langaro is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    thanks for the answer but I'm afraid that I had already tried that and it didn't work, tried it again, doesn't work either

    i just tried to save the first dis.readUTF() in a string and always use the string instead of using the method a second time, that's it

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Print out the values of the Strings out to see what values they actually have.

  5. #5
    langaro is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    oh, I didn't express myself clearly, by 'that's it' I meant that it works now, it was the double use of dis.readUTF() why it didn't work

    if you look at my code you'll see that I had already shown the values of the variables to check if they're identical

    anyways, thanks for your help

Similar Threads

  1. Object Comparison!
    By sajdutt in forum Advanced Java
    Replies: 6
    Last Post: 05-23-2008, 02:48 PM
  2. String comparison
    By abhiN in forum New To Java
    Replies: 2
    Last Post: 04-09-2008, 05:47 AM
  3. Comparison of Strings
    By Cero.Uno in forum New To Java
    Replies: 3
    Last Post: 02-11-2008, 03:46 AM
  4. Date comparison
    By Rageagainst20 in forum New To Java
    Replies: 0
    Last Post: 12-19-2007, 07:34 PM
  5. String comparison
    By sireesha in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 01:16 AM

Posting Permissions

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