Results 1 to 4 of 4
  1. #1
    Ragoune is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default 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:

    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 initialization of the variables used has been done before.

    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

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

    Default

    the line "count ++;" isn't executed
    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.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default

    Java Code:
    	if (line.substring(begin, end).equals("[B]50631700[/B]"))
    	{
    		System.out.println(count);
    		System.exit(0);
    	}
    [50631700][526503000][50631700][526502500][50634600][526498599][50635400][526498500][50639500][526499800][50636599][526504300]Metselaarsstraat

    Won't this exit?

  4. #4
    Ragoune is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default

    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

  1. Replies: 7
    Last Post: 03-05-2011, 09:25 AM
  2. Replies: 1
    Last Post: 02-28-2011, 05:30 PM
  3. Stepping through a text file line by line
    By evanlivingston in forum New To Java
    Replies: 10
    Last Post: 01-29-2011, 04:30 AM
  4. Replies: 8
    Last Post: 12-28-2010, 02:24 PM
  5. Reading & Analyzing a Text file and
    By A.M.S in forum New To Java
    Replies: 1
    Last Post: 11-07-2009, 06:47 PM

Posting Permissions

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