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 loadTeams() {
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, loadPlayers(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[] loadPlayers(String teamName){
Player[] players = new Player[11];
try {
String path = "team_Netherlands.txt"; //works
String path2 = "team_"+teamName+".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_Netherlands.txt
for the path2 variable.
using this will however crash the app. 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)
Why aren't those strings equal and how to make them equal? (i.e., make the non-hardcoded string equal to the hardcoded one)