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:
Quote:
#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.
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?