Results 1 to 5 of 5
Thread: Bug Reading txt files
- 08-13-2010, 03:10 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 5
- Rep Power
- 0
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
here is the functionJava Code:[name] :Monster [description] :Monster Faction [members] :Rat :Imp :Goblin :Minotaur :Dragon [allies] :Summoner :Tammer [enemies] :Player :Npc
here is the output of the functionJava 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 what it should readJava 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
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
- 08-13-2010, 03:11 AM #2
Member
- Join Date
- Aug 2010
- Posts
- 5
- Rep Power
- 0
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.
- 08-13-2010, 01:55 PM #3
Can you explain the difference between what the program does and what you want it to do?
You've posted samples with no explanation.
- 08-13-2010, 09:42 PM #4
Member
- Join Date
- Aug 2010
- Posts
- 5
- Rep Power
- 0
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.
- 08-13-2010, 10:03 PM #5
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:but it seems to skip some values without doing anything to them
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
-
Reading .bin files
By spatel14 in forum New To JavaReplies: 3Last Post: 06-22-2010, 04:39 PM -
Help with reading in a certain types of files
By ShinTec in forum Advanced JavaReplies: 2Last Post: 04-27-2010, 11:09 AM -
Reading .txt files
By cvcs1 in forum New To JavaReplies: 3Last Post: 01-20-2010, 09:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks