This is my first post here and I hope to find some help as I#m not only new to this site but also to Java.
For a game I am using Properties (with a corresponding file) to save and load settings. I also have written a language system where I now get problems (Explained under the code)
Language class: Lang.java :
package de.cozmic.phaseone.reference;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import de.cozmic.phaseone.PhaseOne;
import de.cozmic.phaseone.console.Console;
public class Lang {
private static String path = null;
public static final String en = "en";
public static final String de = "de";
public static final String it = "it";
public static final String fr = "fr";
public static Properties lang = new Properties();
public Lang() {
String language = PhaseOne.settings.getProperty("Language");
//language = de; //FOR TESTING
Console.addLine("Starting Language : " + language);
if (language == en) path = "en.l";
if (language == de) path = "de.l";
if (language == it) path = "it.l";
if (language == fr) path = "fr.l";
Console.addLine("First Tried Language Path : " + path);
if (path == null) path = "en.l";
Console.addLine("Final Language Path : " + path);
try {
InputStream in = Lang.class.getResourceAsStream(path);
lang.load(in);
in.close();
} catch (IOException e) {
Console.addLine(e.getMessage());
e.printStackTrace();
}
}
public static void use(String language) {
try {
FileInputStream in = new FileInputStream(language);
lang.load(in);
in.close();
} catch (IOException e) {
Console.addLine(e.getMessage());
e.printStackTrace();
}
reload();
}
private static void reload() {
//TODO
}
}
Main class: PhaseOne.java
package de.cozmic.phaseone;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import de.cozmic.phaseone.console.Console;
import de.cozmic.phaseone.console.Log;
import de.cozmic.phaseone.display.FLauncher;
import de.cozmic.phaseone.input.InputHandler;
import de.cozmic.phaseone.reference.Lang;
import de.cozmic.phaseone.reference.Reference;
public class PhaseOne {
public static Properties settings = new Properties();
public PhaseOne() {
new Log();
new InputHandler();
loadSettings();
new Lang();
new FLauncher();
}
private void loadSettings() {
File f = new File("settings.p");
if (!f.exists())
try {
f.createNewFile();
FileOutputStream out = new FileOutputStream(f.getAbsolutePath());
settings.setProperty("Language", Lang.en);
settings.setProperty("GameWidth", String.valueOf(Reference.WIDTH));
settings.setProperty("GameHeight", String.valueOf(Reference.HEIGHT));
settings.store(out, null);
out.close();
} catch (IOException e) {
Console.addLine(e.getMessage());
e.printStackTrace();
}
try {
Console.addLine("Loading settings");
FileInputStream in = new FileInputStream(f.getAbsolutePath());
settings.load(in);
in.close();
} catch (IOException e) {
Console.addLine(e.getMessage());
Console.addLine("Error: Loading settings failed!");
e.printStackTrace();
}
Console.addLine("Loaded settings:");
Enumeration<?> enu = settings.propertyNames();
while (enu.hasMoreElements()) {
String set = (String) enu.nextElement();
Console.addLine(set + " : " + settings.getProperty(set));
}
}
public static void changeSettings(String setting, String value) {
settings.setProperty(setting, value);
}
public static void main(String[] args) {
new PhaseOne();
}
}
Basically everything is working correctly and the properties are used correctly to display the text. The problem is that it works as long as I directly use a String variable containing "de", but nut if I let the program read the same (!) value "de" from the properties. In the Lang() Constructer it gets null for the path whereas when I used "de" for the language variable it worked correctly.
This is the settings.p:
#Thu Aug 14 23:33:31 CEST 2014
GameWidth=800
GameHeight=600
Language=de
And this is (the relevant part of) the Log file:
Loading settings
Loaded settings:
Language : de
GameWidth : 800
GameHeight : 600
Starting Language : de
First Tried Language Path : null
Final Language Path : en.l
I hope anyone can tell me what is wrong or give a suggestion what to try to get it working. I looked over it several times and really don't see why it should be behaving like this.
Also in the log you can clearly see that the variable language actually is right, just the if statement doesn't recognize it for whatever reason. Like I said when I directly give it the String value everything works as intended.
Thanks in advance for everyone who is reading this!