I have a file that has 90 objects, each object has 20 attributes. Each line of the file has 1 object so i need to read a line and separate all those 20 attributes so i can fill all the information is need in an object and then put that object in an array of objects.
I found one question here on SO that did something similar but they had a single atribute per line so they just had to read a line and put that line into each atribute, my problem is that i have 20 attributes in one line, how can i separate them?
EDIT 1:
Thanks for everyone that helped but now i got to another problem. my code is the following:
public void loadGang(){
String currentline;
Gang g =new Gang();
String[] result= new String[50];
BufferedReader info;
try {
info = new BufferedReader(new FileReader("Gangs.txt"));
for(int i=0;i<90;i++){
currentline= info.readLine();
result=currentline.split(" ");
g.setName(result[0]);
g.setHire(Integer.parseInt(result[1]));
g.setUpkeep(Integer.parseInt(result[2]));
g.setCombat(Integer.parseInt(result[3]));
g.setDefense(Integer.parseInt(result[4]));
g.setTechlevel(Integer.parseInt(result[5]));
g.setStealth(Integer.parseInt(result[6]));
g.setDetect(Integer.parseInt(result[7]));
g.setChaos(Integer.parseInt(result[8]));
g.setControl(Integer.parseInt(result[9]));
g.setHeal(Integer.parseInt(result[10]));
g.setInfluence(Integer.parseInt(result[11]));
g.setResearch(Integer.parseInt(result[12]));
g.setStrenght(Integer.parseInt(result[13]));
g.setBlade(Integer.parseInt(result[14]));
g.setRange(Integer.parseInt(result[15]));
g.setFighting(Integer.parseInt(result[16]));
g.setMarts(Integer.parseInt(result[17]));
listgang.add(g);
System.out.println(listgang.get(i).getName());
}
info.close();
} catch ( IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I'm having " Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at Game.loadGang(Game.java:30)" So this works for 7 Lines of the text file but it stop there, i think there is some kind of problem with the "currentline= info.readLine();" part because i tried put a print there so i can see if that was working but when it got to that line it didn't made any output so maybe the problem is there? It should be a problem of an array out of bounds but i can't see where, any ideias?
EDIT 2: I guess there's a problem in the file or something because i tried to check if the split was happening by checking the lenght of results and i see that in that line the output was 1 so something is happening in that line...
EDIT 3: I did it! There was a blank line or something on the file i couldn't see in notepad but i opened the .txt file the WordPad and i just erased that line and now it works fine.