-2

I have two string and when i compare these two strings in java using equals method it says has not equal. Not sure why it has to say not equal. Can somebody please help me on issue and advise me here.

Below is the code snippet.

public class Ex9 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        //String str1_ui =    "In Network: $1,550 individual; $3,875 family; includes deductible; copays do not apply; in and out of network combined Out of Network: $2,350 individual; $5,875 family; includes deductible; copays do not apply; in and out of network combined";
        //String str2_excel = "In Network: $1,550 individual; $3,875 family; includes deductible; copays do not apply; in and out of network combined Out of Network: $2,350 individual; $5,875 family; includes deductible; copays do not apply; in and out of network combined";
        
        String str1_ui =    "deductible; copays";
        String str2_excel = "deductible; copays";
        
        System.out.println("Length of str1_ui    :  "+str1_ui.trim().length());
        System.out.println("Length of str2_excel :  "+str2_excel.trim().length());
        
        if(str1_ui.trim().equalsIgnoreCase(str2_excel.trim()))
        {
            System.out.println("Both are equal");
        }
        else
        {
            System.err.println(" Both are not equal");
        }
    }

}

Console output

enter image description here

Chaosfire
  • 4,818
  • 4
  • 8
  • 23
Balaji Singh .Y
  • 683
  • 2
  • 9
  • 22
  • 8
    Cannot reproduce: https://ideone.com/iprWSi - Whatever code it is you think you are running, it isn't the one you posted above. – OH GOD SPIDERS Aug 31 '23 at 15:53
  • Maybe it's some kind of encoding issue? It seems you're getting the strings from a UI and an excel sheet respectively. Maybe they use different encodings for some characters? Maybe try to convert it into a byte array and take a look. – pascalpuetz Aug 31 '23 at 15:56
  • @OH GOD SPIDERS - I have posted the same code – Balaji Singh .Y Aug 31 '23 at 16:02
  • @pascalpuetz : Yes you are right I am comparing strings after reading from excel and another UI – Balaji Singh .Y Aug 31 '23 at 16:04
  • @BalajiSingh.Y I have no idea what you are trying to tell me here. To clarify: I linked to a site that shows that the code you posted does not produce the result you claim it produces. If you visit https://ideone.com/iprWSi you will see in the output section that the code you posted will result in "Both are equal" being printed into the output. – OH GOD SPIDERS Aug 31 '23 at 16:05
  • What @OHGODSPIDERS is implying (I believe) is that while this may be the code in your IDE, it's not necessarily what is currently compiled and running. Delete the compiled versions of your code, and try re-building it. – Rogue Aug 31 '23 at 16:05
  • Sample code for your reference i am posting here - which i use for comparison of data after getting ui and excel String hospital_semi_private_room_from_Excel = ObjectRepo.lhmap_vendor_TestData.get("Hospital Semi-Private Room").toString(); if(hospital_semi_private_room_from_UI.getText().trim().replaceAll("\\s+"," ").equalsIgnoreCase(hospital_semi_private_room_from_Excel.trim().replaceAll("\\s+"," "))) – Balaji Singh .Y Aug 31 '23 at 16:11
  • I did work on another exmaple it works fine but the issue is with this strings . Just thinking it might be some junk/special character. Though the length is same but while comparison it says not equal. – Balaji Singh .Y Aug 31 '23 at 16:16
  • but if you delete the one space between two words and add one space in both the strings , save it and then run now it shows both strings are equal. – Balaji Singh .Y Aug 31 '23 at 16:19
  • How to remove that special or junk character any sample code will help here. – Balaji Singh .Y Aug 31 '23 at 16:20
  • "but if you delete the one space between two words and add one space , save it and then run now it shows both strings are equal." <- That sounds like the spaces might be different. For example one could be a normal whitespace and the other a non breaking space. (https://en.wikipedia.org/wiki/Whitespace_character) – OH GOD SPIDERS Aug 31 '23 at 16:24
  • 3
    some questions/anwers that might help: [How can I replace the no break space (nbsp) with a normal space?](https://stackoverflow.com/questions/21988814/how-can-i-replace-the-no-break-space-nbsp-with-a-normal-space) and [How can I replace non-printable Unicode characters in Java?](https://stackoverflow.com/questions/6198986/how-can-i-replace-non-printable-unicode-characters-in-java) - Maybe this might help you to "normalize" your strings and remove everything that isn't visible by the human eye – OH GOD SPIDERS Aug 31 '23 at 16:25
  • Sure, I will refer to the links suggested by you all. Will give a try and keep you posted. – Balaji Singh .Y Aug 31 '23 at 16:48
  • I tried with all possible alternatives, but nothing worked. can somebody please help ? – Balaji Singh .Y Sep 01 '23 at 04:50

0 Answers0