I've been getting this error while trying to load certain data from a txt file.
public static boolean initalize(String FileName) {
String line = "";
String token = "";
String token2 = "";
String token2_2 = "";
String[] token3 = new String[10];
boolean EndOfFile = false;
BufferedReader characterfile = null;
try {
characterfile = new BufferedReader(new FileReader("./Data/data/"
+ FileName));
} catch (FileNotFoundException fileex) {
Misc.println("File not loaded");
return false;
}
try {
line = characterfile.readLine();
} catch (IOException ioexception) {
return false;
}
while ((EndOfFile == false) && (line != null)) {
**line = line.trim();
int spot = line.indexOf("=");
if (spot > -1) {
token = line.substring(0, spot);
token = token.trim();
token2 = line.substring(spot + 1);
token2 = token2.trim();
token2_2 = token2.replaceAll("\t\t", "\t");
token2_2 = token2_2.replaceAll("\t\t", "\t");
token2_2 = token2_2.replaceAll("\t\t", "\t");
token2_2 = token2_2.replaceAll("\t\t", "\t");
token2_2 = token2_2.replaceAll("\t\t", "\t");
token3 = token2_2.split("\t");**
if (token.equals("drop")) {
int id = Integer.parseInt(token3[0]);
int x = Integer.parseInt(token3[1]);
int y = Integer.parseInt(token3[2]);
int amount = Integer.parseInt(token3[3]);
int height = Integer.parseInt(token3[4]);
globalDrops.add(new GlobalDrop(id,amount,x,y,height));
}
} else {
if (line.equals("[ENDOFDROPLIST]")) {
try {
characterfile.close();
} catch (IOException ioexception) {
}
return true;
}
}
try {
line = characterfile.readLine();
} catch (IOException ioexception1) {
EndOfFile = true;
}
}
try {
characterfile.close();
} catch (IOException ioexception) {
}
return false;
}
However, its been giving me this error:
[8/3/12 5:24 PM]: Exception in thread "main" [8/3/12 5:24 PM]: java.lang.NumberFormatException: For input string: "1923 3208 3214 1 0 2 "
This is how the text file is formated:
sque = 1923 3208 3214 1 0 2 Square
Why is this giving me the error? Is it something to do with the /t/ splits?
Thanks
This is the working one:
sque = 1923 3208 3214 1 0 2 Square
However, I'm trying to load over 400 of these and it would be painful to change all of them at once
Replace all this
token = line.substring(0, spot);
token = token.trim();
token2 = line.substring(spot + 1);
token2 = token2.trim();
token2_2 = token2.replaceAll("\t\t", "\t");
token2_2 = token2_2.replaceAll("\t\t", "\t");
token2_2 = token2_2.replaceAll("\t\t", "\t");
token2_2 = token2_2.replaceAll("\t\t", "\t");
token2_2 = token2_2.replaceAll("\t\t", "\t");
token3 = token2_2.split("\t");**
with
String[] cmd = line.split("=");
token = cmd[0].trim;
String[] numbers = cmd[1].split("\\s+");
The array numbers
will contains the numeric string tokens, already trimmed and ready to be interpreted by Integer.parseInt()
.