Results 1 to 9 of 9
  1. #1
    HeavyTech is offline Member
    Join Date
    Jun 2014
    Posts
    30
    Rep Power
    0

    Default Reading a file and extracting Data

    Hello, I am supposed to read in a file and extract the data.
    Now, I am having a brain fart but can't figure it out . I am trying to add a Long into an Array that I initialized



    so this is how my method looks like

    Java Code:
    public   void readOSMWay(File osm){
    		
    		
    		try {
    			Scanner output = new Scanner(osm);
    			while(output.hasNext()){
    				String line = output.nextLine().replace("^\\s+","");
    				if(line.startsWith(" <way")){
    				    Road way = new Road(line,true);
    					System.out.println("Way ID: " + way); //Testing 
    					}
    				else if(line.startsWith("  <nd")){
    					this.ref.add(OSM.extractStringFromVal(line, "ref"));
    					
    				}
    				}
    			
    			output.close();
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();}
    
    
    
    	}
    I am having problem with the else if block. I am supposed to add all the Long values into the array.
    error:
    Java Code:
    The method add(Long) in the type ArrayList<Long> is not applicable for the arguments (String)

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Reading a file and extracting Data

    Are you converting the long value from the string into an actual long? If you have declared something like ArrayList<Long> then you cannot
    add a string to it. Also, what is OSM? Is it the same as osm?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    HeavyTech is offline Member
    Join Date
    Jun 2014
    Posts
    30
    Rep Power
    0

    Default Re: Reading a file and extracting Data

    Thank you for reply.

    "osm" is actual file that contains latitude, longitude, id , timestamp, username...etc
    But all we need is id(Long) latitude and Longitude ( Which makes a Point) .

    OSM is another class . I have a method extractStringFromVal which just parses each line.

    So the OSM takes in a String and extracts the value that I want...Now, I want this value to be a Long

  4. #4
    HeavyTech is offline Member
    Join Date
    Jun 2014
    Posts
    30
    Rep Power
    0

    Default Re: Reading a file and extracting Data

    ah crap!!!

    I just figured it out Jim. I read your response carefully . I needed to parse that String!


    Java Code:
    this.ref.add(Long.parseLong(OSM.extractStringFromVal(line, "ref")));

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Reading a file and extracting Data

    Quote Originally Posted by HeavyTech View Post
    I just figured it out Jim. I read your response carefully . I needed to parse that String!
    Exactly! Nicely done.

    BTW, there is a String method called trim() which returns the string with leading and trailing white space removed.

    Edit: Upon further reflection, it appears you are in fact removing leading spaces from the line. So I am not certain how the startsWith() method will ever return true since your test tokens contain leading white space.

    Regards,
    Jim
    Last edited by jim829; 11-15-2014 at 03:04 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    HeavyTech is offline Member
    Join Date
    Jun 2014
    Posts
    30
    Rep Power
    0

    Default Re: Reading a file and extracting Data

    Yeah, my OSM class removes the white spaces. Thanks for the Advice!

    So this
    Java Code:
    String[] array = line.split("\\s+");
    is the same as
    Java Code:
    String[] array = line.trim();
    So much to learn. Thank you
    Last edited by HeavyTech; 11-15-2014 at 04:00 AM.

  7. #7
    HeavyTech is offline Member
    Join Date
    Jun 2014
    Posts
    30
    Rep Power
    0

    Default Re: Reading a file and extracting Data

    Hi once again ;

    I am still working on the same project as about so..I figured I could add to this.

    So, I modified my reader, but I having problem printing the values of my hashmap[ Keys print correctly]

    so this is my method to print my hashmap


    This just prints my keys: I would like to print my values.
    Java Code:
    	public void dumpWays(){
    
    		for(Long value : ways.keySet()){
    			System.out.println(ways.get(value));
    			
    		}
    Now, my values are actually supposed to be a "Road"

    so I tried this

    Java Code:
    public void dumpWays(){
    
    		for(Road value : ways.keySet()){
    			System.out.println(ways.get(value));
    			
    		}
    But I keep getting error
    Java Code:
    Type mismatch: cannot convert from element type Long to Road

    Just in case This is my reader
    Java Code:
    public  HashMap<Long,Road> ReadOSMWays(File osm){
    		
    		try{
    
    			Scanner output = new Scanner(osm);
    			while(output.hasNext()){
    				String line = output.nextLine().replace("^\\s+","");
    				if(line.startsWith(" <way")){
    					Road road = new Road(line,true);
    					while(!line.contains("</way")){
    						line = output.nextLine();
    						if(line.contains("<nd")){
    							long ref = Long.parseLong(OSM.extractStringFromVal(line, "ref"));
    							Point p = nodes.get(ref);
    							assert (p != null);
    							road.points.add(p);
    						}
    					}
    					ways.put((long) road.getId(),road);	
    
    
    				}}} catch (FileNotFoundException e) {
    					e.printStackTrace();
    
    				}
    
    		return ways;
    	}

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Reading a file and extracting Data

    What makes you believe a keySet returns values and not keys? The operative word is keySet.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    HeavyTech is offline Member
    Join Date
    Jun 2014
    Posts
    30
    Rep Power
    0

    Default Re: Reading a file and extracting Data

    oh...
    yes! Thank you

Similar Threads

  1. Reading data from file
    By Roberto1989 in forum New To Java
    Replies: 1
    Last Post: 04-28-2011, 06:52 PM
  2. Extracting and displaying data from a text file
    By fugazi in forum New To Java
    Replies: 4
    Last Post: 01-07-2011, 07:37 PM
  3. Reading data to file
    By puk284 in forum Advanced Java
    Replies: 1
    Last Post: 04-28-2009, 04:19 PM
  4. Reading Data from a file
    By ramachandran in forum New To Java
    Replies: 2
    Last Post: 10-24-2007, 08:22 AM
  5. Extracting data from an XML file...
    By techno_brains in forum New To Java
    Replies: 1
    Last Post: 07-15-2007, 06:46 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
  •