Results 1 to 3 of 3
  1. #1
    adwodon is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default Identifying comments in data with a hash. Problem with delimiter?

    Hi there,
    Its been a while since I've used Java and I'm having trouble extracting data which uses a hash to identify a new set of data

    A data set would look like this:

    #BASEMENT_ANTENNA

    OK
    at+csq
    +CSQ: 3,99

    OK
    at+csq
    +CSQ: 3,99

    OK
    at+csq
    +CSQ: 7,99

    OK
    at+csq
    +CSQ: 7,99

    OK
    at+csq
    +CSQ: 7,99

    OK
    at+csq
    +CSQ: 7,99
    The #BASEMENT part recommends the location, I have 2 objects to store this data, one is called DataPoint and contains the two numbers for each reading (7 & 99 for instance) and then a DataSet object which contains multiple DataPoints and also a location which I want to be #BASEMENT_ANTENNA.
    What I would have is a notepad file with a load of these readings from multiple areas and so I need Java to Identify when a new #LOCATION pops up, make a new DataSet and fill it with DataPoints until the next #LOCATION

    Here is my test class which I've used to try and test this method before inserting it into the rest of the classes which would extract the data from a text file rather than a string, and has other methods to analyse.

    Java Code:
    package dataProcessing;
    
    import java.util.*;
    
    public class TestClass {
    
    	public static Map<String, DataSet> importFile(){
    		String b = "#DOWN_THE_LIFT\\n\\nOK\\nat+csq\\n+CSQ: 14,99\\n\\nOK\\nat+csq\\n+CSQ: 14,99\\n\\nOK\\nat+csq\\n+CSQ: 14,99\\n\\n#DOWN_THE_STAIRS\\n\\nOK\\nat+csq\\n+CSQ: 14,99\\n\\nOK\\nat+csq\\n+CSQ: 14,99\\n";
    
    
    		Scanner s = new Scanner(b).useDelimiter("[,\\s\\n]");
    		Map<String, DataSet> myData = new HashMap<String, DataSet>();
    		char hashChar = '#';
    		String hashString = Character.toString(hashChar);
    		while(s.hasNextLine()){
    			double pass = 1;
    			s.findInLine(hashString);
    			String loc = s.next();
    			pass = 1;
    			DataSet data = new DataSet();
    			System.out.println(loc);
    			data.setLocation(loc);
    			while(pass==1){
    				if(s.next().contains(hashString)!=true){
    					double RSSI = s.nextDouble();
    					double BER = s.nextDouble();
    					DataPoint dp = new DataPoint();
    					dp.setDataPointRSSI(RSSI);
    					dp.setDataPointBER(BER);
    					data.addDataPoint(dp);
    					s.next();
    				}
    				else{
    					pass = 0;
    				}
    				myData.put(data.getLocation(), data);
    			}
    			
    		}
    		return myData;
    	}
    	
    	public static void main(String[] args) {
    		Map<String, DataSet> testData = new HashMap<String, DataSet>();
    		testData = importFile();
    		System.out.println("HashMap: ");
    		Iterator it = testData.keySet().iterator();
    		while(it.hasNext()){
    			String key = it.next().toString();
    			String value = testData.get(key).toString();
    			System.out.println(key+" "+value);
    		}
    	}
    }

    After testing my first pitfall appears to be in the delimiter, I may have some other errors which I havent seen yet but first things first, the scanner seems to be making tokens which are only paying attention to whitespace and the comma not the newline.

    What have I done wrong?

  2. #2
    adwodon is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    Ok this can be put as solved, figured it out myself, a tonne of newbie mistakes!

    If anyone is curious (if they search for this in future) this is what my working code looks like (all the printing was purely to help me figure out what was going on at what stage).

    Java Code:
    	public static Map<String, DataSet> readFile(){
    		
    		
    		String br = "OKA\nat+csq\n+CSQ: 14,99\n#DOWN_THE_LIFT\n\nOKB\nat+csq\n+CSQ: 14,99\n\nOK\nat+csq\n+CSQ: 15,98\n\nOK\nat+csq\n+CSQ: 13,97\n\n#DOWN_THE_STAIRS\n\nOKC\nat+csq\n+CSQ: 4,99\n\nOK\nat+csq\n+CSQ: 1,99\n";
    		//System.out.println(b);
    
    		Scanner s = new Scanner(br).useDelimiter(",|\\s+|\n|\r\n|\r");
    		Map<String, DataSet> myData = new HashMap<String, DataSet>();
    		char hashChar = '#';
    		String hashString = Character.toString(hashChar);
    		String check = s.next();
    		while(s.hasNextLine()){
    			String loc = null;
    			double pass = 1;
    			while(pass==1 && s.hasNextLine()){
    				if(check.contains(hashString)){
    					loc = check;
    					pass = 0;
    					System.out.println(check+" HOLLA!");
    				}
    				else{
    					System.out.println(check+" BOO!");
    					System.out.println(s.hasNextLine());
    					check=s.nextLine();
    				}
    			}
    			pass = 1;
    			DataSet data = new DataSet();
    			System.out.println(loc);
    			data.setLocation(loc);
    			while(pass==1 && s.hasNextLine()){
    				if(check.contains(hashString)){
    					pass=0;
    				}
    				else{
    					if(s.hasNextDouble()){
    						double RSSI = s.nextDouble();
    						System.out.println("****"+RSSI+"****");
    						double BER = s.nextDouble();
    						System.out.println("**!!"+BER+"!!**");
    						DataPoint dp = new DataPoint();
    						dp.setDataPointRSSI(RSSI);
    						dp.setDataPointBER(BER);
    						data.addDataPoint(dp);
    					}
    					else{
    					}
    				}
    				myData.put(data.getLocation(), data);
    				check = s.next();
    			}
    
    		}
    		return myData;
    	}

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    If anyone is curious (if they search for this in future) this is what my working code looks like
    Thanks for sharing.

    db

Similar Threads

  1. Identifying sender on keypress event
    By tomtraxler in forum NetBeans
    Replies: 1
    Last Post: 01-08-2011, 04:49 PM
  2. Help me ! identifying the mistake
    By o0oNorao0o in forum New To Java
    Replies: 6
    Last Post: 11-06-2010, 10:03 PM
  3. Most of my trouble is identifying a pattern
    By ElkNinja in forum New To Java
    Replies: 4
    Last Post: 10-10-2010, 06:17 AM
  4. help with identifying components of a gui
    By jaytee in forum New To Java
    Replies: 1
    Last Post: 03-08-2010, 02:28 AM
  5. Problem with formating comments
    By Lubomirus in forum Eclipse
    Replies: 0
    Last Post: 07-07-2009, 08:37 AM

Tags for this Thread

Posting Permissions

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