LGunit LGunit - 6 months ago 16
Android Question

Strings contain the same characters but are still different

this is my first post, so please excuse me if I do anything wrong(feel free to correct me on anything)

I am trying to read a .txt file and use each sentence as a name for a team, and at the same time use that name to seek out another .txt file to get its content. All the .txt files are at the root of my assets folder. The first .txt file works fine, I use assetmanager.open and readLine() to obtain the string, but when using that string as a parameter to get the second .txt I get a java.io.FileNotFoundException. However, when calling that same .txt file with a hardcoded String, everything works fine. Upon further inspection I found out that the hardcoded string and the one used as a parameter return false after using the equals() function on it.

This is the method calling the first.txt

private void load_Teams() {

try {

BufferedReader r = new BufferedReader(new InputStreamReader(assetManager.open("matches.txt")));
String name, bio, trainer;
for(int i = 0; i < 4; i++){
name = r.readLine();
bio = r.readLine();
trainer = r.readLine();
System.out.println(name+", "+bio+", "+trainer);
teams[i] = new Team(name, bio, i, load_Players(name), trainer);
}
r.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}


using "name" as a parameter for the following method:

private Player[] load_Players(String team_Name){
Player[] players = new Player[11];

try {

String path = "team_Netherlands.txt"; //works
String path2 = "team_"+team_Name+".txt"; //doesn't work?
System.out.println("are "+path+" and " +path2 +" the same? "+path.equals(path2));

BufferedReader r = new BufferedReader(new InputStreamReader(assetManager.open(path2)));

//perform operations on the obtained info
}
r.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return players;
}


The first sentence in the file is "Netherlands" (without the quotes)
which I think should lead to team_Neterlands.txt for the path2 variable.
using this will however crash the app. when using the path variable, it works just fine. the println confirms that the strings are not equal.(see first sentence of the logcat)

logcat:

05-26 11:18:23.152 2960-2960/com.myname.testapp I/System.out: are team_Netherlands.txt and team_Netherlands.txt the same? false
05-26 11:18:23.152 2960-2960/com.myname.testapp W/System.err: java.io.FileNotFoundException: team_Netherlands.txt
05-26 11:18:23.152 2960-2960/com.myname.testapp W/System.err: at android.content.res.AssetManager.openAsset(Native Method)
05-26 11:18:23.152 2960-2960/com.myname.testapp W/System.err: at android.content.res.AssetManager.open(AssetManager.java:354)
05-26 11:18:23.152 2960-2960/com.myname.testapp W/System.err: at android.content.res.AssetManager.open(AssetManager.java:328)
05-26 11:18:23.152 2960-2960/com.myname.testapp W/System.err: at com.myname.testapp.Poule_Activity.load_Players(Poule_Activity.java:144)
05-26 11:18:23.152 2960-2960/com.myname.testapp W/System.err: at com.myname.testapp.Poule_Activity.load_Teams(Poule_Activity.java:94)
05-26 11:18:23.152 2960-2960/com.myname.testapp W/System.err: at com.myname.testapp.Poule_Activity.onCreate(Poule_Activity.java:53)
05-26 11:18:23.152 2960-2960/com.myname.testapp W/System.err: at android.app.Activity.performCreate(Activity.java:5990)
05-26 11:18:23.152 2960-2960/com.myname.testapp W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
05-26 11:18:23.152 2960-2960/com.myname.testapp W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
05-26 11:18:23.152 2960-2960/com.myname.testapp W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
05-26 11:18:23.152 2960-2960/com.myname.testapp W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:156)
05-26 11:18:23.152 2960-2960/com.myname.testapp W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
05-26 11:18:23.152 2960-2960/com.myname.testapp W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
05-26 11:18:23.152 2960-2960/com.myname.testapp W/System.err: at android.os.Looper.loop(Looper.java:211)
05-26 11:18:23.153 2960-2960/com.myname.testapp W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5373)
05-26 11:18:23.153 2960-2960/com.myname.testapp W/System.err: at java.lang.reflect.Method.invoke(Native Method)
05-26 11:18:23.153 2960-2960/com.myname.testapp W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
05-26 11:18:23.153 2960-2960/com.myname.testapp W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
05-26 11:18:23.153 2960-2960/com.myname.testapp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)


Can anyone please tell me why those strings are not equal and how to make them equal? (i.e., make the non-hardcoded string equal to the hardcoded one)
thank you in advance!

SOLVED

In case anyone else encounters this problem: the error was with the encoding, in my case my text files were encoded in UTF-8 BOM instead of UTF-8

Thank you everyone!

Answer

The team_name you read from file contains UTF-8 byte order mark octets in front

ef bb bf

and they are not visible as such in log output.

Either save the files without BOM, or remove the BOM in your code.