Results 1 to 5 of 5
  1. #1
    MHardeman25 is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default Bug Reading txt files

    Hey guys, I have a problem. I was just starting to write a simple little game in java to get myself back into a programming mood for the coming semester. I am trying to write a function that will read a text file and output the data into a monstrosity of a data set. a List[2] that contains a List<String> and a List<List>. Don't worry about that last bit, all I want to know is. What is my line reader doing wrong.

    here is the text file
    Java Code:
    [name]
    :Monster
    
    [description]
    :Monster Faction
    
    [members]
    :Rat
    :Imp
    :Goblin
    :Minotaur
    :Dragon
    
    [allies]
    :Summoner
    :Tammer
    
    [enemies]
    :Player
    :Npc
    here is the function
    Java Code:
         /*
         * Simple text file reading method.
         *
         * Takes an hopefully valid input file path and uses scanner to read it.
         * This will hopefully make adding new factions as easy as adding a new
         * faction.txt to the factions folder.
         */
        public List[] readTextFile(File file) throws IOException {
    
            List[] output = new List[2];
    
            List<String> vars = new ArrayList<String>();
    
            List<List> vals = new ArrayList<List>();
            List<String> temp = new ArrayList<String>();
    
            FileInputStream fileStream = new FileInputStream(file);
            DataInputStream dataStream = new DataInputStream(fileStream);
            BufferedReader reader = new BufferedReader(new FileReader(file));
    
            String line = "";
            int val_count = 0;
    
            do {
                System.out.println(line);
                if(line.startsWith("[")){
                    line = line.substring(1,line.indexOf("]")-1);
                    vars.add(line);
                    System.out.println("var");
                } else if(line.startsWith(":")) {
                    if(val_count < vars.size()){
                        vals.add(temp);
                        temp = new ArrayList<String>();
                        val_count++;
                        System.out.println("val");
                    }
                    line = line.substring(line.indexOf(":")+1);
                    temp.add(line);
                } else if(line.equals("")){
                    System.out.println("blank");
                } else {
                    System.out.println("something else happened");
                }
            } while ((line = reader.readLine()) != null);
    
            output[0] = vars;
            output[1] = vals;
    
            return output;
        }
    here is the output of the function
    Java Code:
    blank
    [name]
    var
    :Monster
    val
    
    blank
    [description]
    var
    :Monster Faction
    val
    
    blank
    [members]
    var
    :Rat
    val
    :Imp
    :Goblin
    :Minotaur
    :Dragon
    
    blank
    [allies]
    var
    :Summoner
    val
    :Tammer
    here is what it should read
    Java Code:
    blank
    [name]
    var
    :Monster
    val
    
    blank
    [description]
    var
    :Monster Faction
    val
    
    blank
    [members]
    var
    :Rat
    val
    :Imp
    val
    :Goblin
    val
    :Minotaur
    val
    :Dragon
    
    blank
    [allies]
    var
    :Summoner
    val
    :Tammer
    val
    
    blank
    [enemies]
    var
    :Player
    val
    :Npc
    val

  2. #2
    MHardeman25 is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    Yes, I know it's a mess. I am just focused on getting it to work. Is it something to do with the newline character at the end? it seems to want to count many of seperate lines as one line.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    Can you explain the difference between what the program does and what you want it to do?
    You've posted samples with no explanation.

  4. #4
    MHardeman25 is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    Ok. The text file is fairly simple. It has a variable name in brackets [] and the value i want to assign to it under it with a : in front. Basically the reader should move through it one line at a time, and see if it starts with a [ or a : Then it adds it to the proper array list.
    For most values it seems ok, but it seems to skip some values without doing anything to them.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    but it seems to skip some values without doing anything to them
    To see why the lines are skipped you need to to debug your problem some more, you should increase the info in println() statements. For example:
    System.out.println("var");
    should be:
    System.out.println("var, line=" + line + "<");
    and
    System.out.println("line=" + line + "<");

    What are the values being skipped? Why are they skipped and not others?

    What is the variable: temp used for?
    Last edited by Norm; 08-13-2010 at 10:07 PM.

Similar Threads

  1. Reading .bin files
    By spatel14 in forum New To Java
    Replies: 3
    Last Post: 06-22-2010, 04:39 PM
  2. Help with reading in a certain types of files
    By ShinTec in forum Advanced Java
    Replies: 2
    Last Post: 04-27-2010, 11:09 AM
  3. Reading .txt files
    By cvcs1 in forum New To Java
    Replies: 3
    Last Post: 01-20-2010, 09:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •