I am creating a 3d game in Java and I want to be able to take in a players information, and save it to a text file. I understand a database would probably be more optimal, but for my own testing purposes, the text file should fit my needs. My text file includes the following:
username=kyle13524,
player_health=100,
player_mana=100,
player_position_x=100,
player_position_y=100,
player_position_z=100,
player_money=2149000
I am using this parsePlayerData method:
private void parsePlayerData() {
String filestring = null;
List<String> playerData = new ArrayList<String>();
ListIterator<String> li = playerData.listIterator();
try {
BufferedReader br = new BufferedReader(new FileReader("res/data/playerdata.txt"));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
filestring = sb.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(filestring != null) {
String[] parts = filestring.split(",");
String[] str = null;
for(String part : parts) {
playerData.add(part);
}
for(String string : playerData) {
str = string.split("=");
if(str[0] == "username") {
PLAYER_NAME = str[1];
}
if(str[0] == "player_health") {
PLAYER_HEALTH = Integer.parseInt(str[1]);
}
if(str[0] == "player_mana") {
PLAYER_MANA = Integer.parseInt(str[1]);
}
if(str[0] == "player_position_x") {
PLAYER_X = Float.parseFloat(str[1]);
}
if(str[0] == "player_position_y") {
PLAYER_Y = Float.parseFloat(str[1]);
}
if(str[0] == "player_position_z") {
PLAYER_Z = Float.parseFloat(str[1]);
}
}
}
}
Basically, the idea here is to have the data inputted, split into an array by the '=' and organized into static constants:
public static String PLAYER_NAME;
public static int PLAYER_HEALTH;
public static int PLAYER_MANA;
public static float PLAYER_X;
public static float PLAYER_Y;
public static float PLAYER_Z;
public static int PLAYER_MONEY;
The problem is that str[0] isnt being recognized as the values, yet when I print the values to the screen, they are exactly the same as what the if statements are looking for. Can anyone give me an idea as to what is going wrong here? Am I going down the right path?