0

Previously running a file load in Netbeans and grabbing the String entries for comparison produced predictable results:

(Over-simpliflied to get to the heart of the problem.) File 1: UTF-8 Encoded File 2: ISO-8859-1 Encoded

NETBEANS

String strFromFile1 = "A - B"; String strFromFile2 = "A - B";

(strFromFile1 == strFromFile2) evaluated to true.

However, after moving the project to Eclipse, I noticed sometimes the strings value would change, and only for one of my many files, sometimes stripping out the dash (-), with no discernible pattern:

ECLIPSE

String strFromFile1 = "A B"; String strFromFile2 = "A - B";

(strFromFile1 == strFromFile2) evaluated to false.

The comparison is correct, but why did the first string change? The source text files did not change. They both contained the dash. However, the character encoding of each file was different.

Why did this happen? Why is Eclipse choosing to ignore certain characters from a specific file? Other files of the same encoding types (ISO-8859-1, UTF-8) did not experience this issue.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

0

I suspected the issue had to do with NetBeans vs Eclipse environment character encoding.

However, Changing the Run Configuration to Run As > Other > Encoding > UTF-8, had no effect on the issue.

The setting in Eclipse had to be changed at the General level:

Window > Preferences > Text File Encoding > Other > UTF-8

After changing this setting, the issue was resolved, and all tests using string comparisons passed as expected (as it was in NetBeans before migration to Eclipse).

  • Note that using `==` to compare strings will **not** work in general, read http://stackoverflow.com/q/513832/2670892 – greg-449 Oct 26 '16 at 20:20
  • Yes, of course. I simplified the explanation. But I was in fact using a custom DeepEquals function which checks for null values as well as uses the equals() method. – TinkerTenorSoftwareGuy Oct 28 '16 at 19:29