Results 1 to 4 of 4
Thread: Analyzing a line of a text file
- 06-29-2011, 02:31 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 5
- Rep Power
- 0
Analyzing a line of a text file
Hi,
I'm quite new to Java, but I've already completed an Android project. I now started on a Java project, but an error has been occuring for a long time. I have tried to find the reason, but couldn't succeed. As my last resource I would like to ask you if you know what might go wrong.
I basically have a text file, consisting out of many X and Y points enclosed by square brackets. A name can be added behind the points, but is not always the case. A fragment:
[50599800][526496200][50595100][526503000]Hogerbeetsstraat
[50607053][526488901][50607585][526488173][50601960][526486652][50601426][526487378][50607053][526488901]
As you've probably guessed, I want to read the numbers and additional names into my application. To do this, I've written this relatively small piece of code:
The initialization of the variables used has been done before.Java Code:FileReader file = new FileReader("C:\\maps2\\"+i+"\\"+i2+".mada"); BufferedReader reader = new BufferedReader(file); while (reader.ready()) { line = reader.readLine(); line2 = line; ways[ways_length] = new double[line.replaceAll("[^\\[]", "").length()]; count = 0; while (line.contains("[")) { begin = line.indexOf("[")+1; end = line.indexOf("]"); if (line.substring(begin, end).equals("50631700")) { System.out.println(count); System.exit(0); } ways[ways_length][count] = Double.parseDouble(line.substring(begin, end))/lonlat_multiplier; line = line.replace(line.substring(begin-1, end+1), ""); count ++; } //System.out.println(ways[ways_length].length); if (line.length() > 0) { ways_name[ways_length] = line; if (ways_name[ways_length].equals("Metselaarsstraat")) { System.out.println("<"+ways_length+"|"+ways[ways_length][2]+"|"+ways[ways_length][3]); System.out.println(line2); System.out.println(ways[ways_length].length); } } ways_length ++; } file.close(); reader.close();
The problem:
Everything's read correctly, and even displayed correctly (the code for that, I haven't shown). However, at a few lines (each run the same ones) it looks like count remains the same. In other words, the line "count ++;" isn't executed (so it looks).
This is one of the lines where it goes wrong:
[50631700][526503000][50631700][526502500][50634600][526498599][50635400][526498500][50639500][526499800][50636599][526504300]Metselaarsstraat
ways[ways_length][2] is first set to 5.0631700 then 1 loop later to 52.6502500 although this value should be assigned to ways[ways_length][3].
I can't figure out why this is happening and I'm hoping one of you could help me. If you need more information I'd be happy to add some more.
Regards,
Ragoune
- 06-29-2011, 02:35 PM #2
try debugging your code to see why that line is not being executed. Print out the values of the variables in the if statement that controls when the statement will be executed. Perhaps it is never true.the line "count ++;" isn't executed
- 06-29-2011, 03:21 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
[50631700][526503000][50631700][526502500][50634600][526498599][50635400][526498500][50639500][526499800][50636599][526504300]MetselaarsstraatJava Code:if (line.substring(begin, end).equals("[B]50631700[/B]")) { System.out.println(count); System.exit(0); }
Won't this exit?
- 06-29-2011, 03:54 PM #4
Member
- Join Date
- Jun 2011
- Posts
- 5
- Rep Power
- 0
Woow, thanks for the reply. Of course, I've been debugging for a long time, but you gave me a new insight:
The problem was that the "line = line.replace(...);" also deleted the other occurences of the point. I thought it only deleted the first one.
It was pure a coincidence that the 2 points where exactly the same. Thanks!
Btw, for anyone who's interested, this is my new line:
line = line.replaceFirst(line.substring(begin-1, end+1).replace("[", "\\[").replace("]", "\\]"), "");
Similar Threads
-
Transforming xml to text keeps placing blank line at beginning of text file
By DerekRaimann in forum Advanced JavaReplies: 7Last Post: 03-05-2011, 09:25 AM -
Taking a line from a text file if it contains the specified text
By daoping in forum New To JavaReplies: 1Last Post: 02-28-2011, 05:30 PM -
Stepping through a text file line by line
By evanlivingston in forum New To JavaReplies: 10Last Post: 01-29-2011, 04:30 AM -
Reverse search a Text file from last line to top line
By Jman85 in forum New To JavaReplies: 8Last Post: 12-28-2010, 02:24 PM -
Reading & Analyzing a Text file and
By A.M.S in forum New To JavaReplies: 1Last Post: 11-07-2009, 06:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks