-5

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.

I3ryI3e
  • 3
  • 3
  • What have you done so far? Can you give some examples? Please ask more specific questions. – Turing85 May 18 '16 at 10:24
  • Hey man, you know what to do. But this is not the place for code requests. – Murat Karagöz May 18 '16 at 10:24
  • if i knew what to do i wouldn't be asking since that's a waste of time... i checked this post http://stackoverflow.com/questions/23145416/creating-objects-via-txt-file-into-an-array-in-java, and i need to do something similar but he has an attribute per line and i have all my attributes in one line, i just need someone to tell me a method or something that i need to separate stuff when i read the info. Or should i just read one entire line to a String and then take stuff from the string to the attributes i need? I'm sorry if i'm not explaining myself well but i don't know much about java yet – I3ryI3e May 18 '16 at 10:29
  • To "separate stuff", you "split" your input. How, you need to do the thinking. If it's a string, you can use a delimiter to split. If it's binary, you need to know the record format and parse the record. – Aakash May 18 '16 at 10:34
  • Thank's for your help. – I3ryI3e May 18 '16 at 10:43

1 Answers1

0

My advice to you would be to create a custom object reader. The custom object reader will read in the object and separate the data of the object into some data structure (array etc). The object could possibly return that structure. You could use this object reader once for each line. When the data structure is returned after each object read, you can do with it what you want. You can of course take this idea and modify it to your liking. If you like this response accept it.

jillian
  • 52
  • 6
  • When you talk about a creating a custom object reader, you're talking about creating a method that reads an entire line to a string and then split that line in every piece of information i need right? – I3ryI3e May 18 '16 at 10:57
  • It could be that if you want. You said that each line is an object, so I mean a method that takes in an object. But if you have a line of text that needs to be split up then yes you can read a line an split it in the method. – jillian May 18 '16 at 11:19
  • Can you help me with the problem i have now? – I3ryI3e May 19 '16 at 12:09