0

I am receiving a packet such as

"c~ ¹" M:0013A20040559361 Ax:48 Ay:10 Az:1012 Tp:25 B:45 ? Tm:Wednesday, 02/01/13 - 16:16.57 Sº~"

and others characters which could not be pasted. I have to read the values after the colons. For example, M: , Ax:, Ay: and store it or draw it in a graph. I know of two ways: StreamTokenizer or simple java logic. The problem is I have tried both, but still I receive too many errors while reading the values. Plus, I tried a string garbage collector, its not working also.

Below are the code for both:

if(message2.contains("Ax:"))
    {
    int index = message2.indexOf("Ax:");
    String Ax = message2.substring(index+3);
    ChartAcceleration.tsAx.addOrUpdate(new Millisecond(), Double.valueOf(Ax));
    }

StreamTokenizer Code:

StreamTokenizer st = new StreamTokenizer(
                new StringReader(message));

         boolean eof = false;
        while(st.nextToken() != StreamTokenizer.TT_EOF){

              int token = st.nextToken();
              switch (token) {
                 case StreamTokenizer.TT_WORD:
                     String word = st.sval;
                     if(word.contains("Ax"))
                     {
                         Ax = true;
                     }
                     else
                         if(word.contains("Ay"))
                     {
                         Ay = true;
                     }
                         else
                             if(word.contains("Az"))
                         {
                             Az = true;
                         }
                             else
                                 if(word.contains("P"))
                             {
                                 P = true;

                             }
                                 else
                                     if(word.contains("B"))
                                 {
                                     B = true;
                                 }
                                     else
                                         if(word.contains("Tp"))
                                     {
                                         Tp = true;
                                        }
                                         else
                                         {
                                             Ax = false;
                                             Ay = false;
                                             Az = false;
                                             P = false;
                                             B = false;
                                             Tp = false;
                                         }
                 //   System.out.println("Word: " + word);
                    break;
                 case StreamTokenizer.TT_NUMBER:
                     double number = st.nval;
                     if(Ax)
                     {
                         Ax = false;
                    //   errorChecker(AxStr,number);
                        AxStr = number;
                         Sender.publishAccelerator("Ax:" + String.valueOf(AxStr));
                     }
                     else
                         if(Ay)
                         {
                             Ay = false;
                             AyStr = number;
                             Sender.publishAccelerator("Ay:"+String.valueOf(AyStr));
                         }
                         else if(Az)
                         {
                             Az = false;
                             AzStr = number;
                             Sender.publishAccelerator("Az:"+String.valueOf(AzStr));
                            // System.out.println("Az:"+AzStr);

                         }
                         else
                            if(P)
                                 {
                                     P = false;
                                     PStr = number;
                                     Sender.publishPressure(String.valueOf(PStr));
                                     //System.out.println("P:"+PStr);

                                 }
                                 else
                                     if(B)
                                     {
                                         B = false;
                                         BStr = number;
                                         Sender.publishBattery(String.valueOf(BStr));
                                     }
                                     else
                                         if(Tp)
                                         {
                                             Tp = false;
                                             TpStr = number;
                                             Sender.publishTemp(String.valueOf(TpStr));
                                         }
                    break;

                 default:
              }
        }
Vinay
  • 6,891
  • 4
  • 32
  • 50
Kamran
  • 152
  • 2
  • 13

1 Answers1

1

I put together a string parsing program for your text.

You define the tokens and end tokens that you want to parse.

Here are my results.

Ay: 10
Az: 1012
Tp: 25
B: 45
Ax: 48
Tm: Wednesday, 02/01/13 - 16:16.57

And here's the parsing code.

import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

public class StringParsing {

    private String[][] tokens = { {"Ax:", " "}, 
            {"Ay:", " "}, {"Az:", " "}, {"Tp:", " "}, {"B:", " "},
            {"Tm:", " Sº"} };

    public Properties parseString(String s) {
        Properties p = new Properties();

        for (int i = 0; i < tokens.length; i++) {
            String value = getValue(s, tokens[i][0], tokens[i][1]);
            p.setProperty(tokens[i][0], value);
        }
        return p;
    }

    private String getValue(String s, String token, String endToken) {
        int sPos = s.indexOf(token);
        if (sPos >= 0) {
            int ePos = s.indexOf(endToken, sPos + 1);
            if (ePos > sPos) {
                sPos += token.length();
                return s.substring(sPos, ePos);
            }
        }
        return "";
    }


    public static void main(String[] args) {
        String s = "c~ ¹\" M:0013A20040559361 Ax:48 Ay:10 Az:1012 " + 
                "Tp:25 B:45 ? Tm:Wednesday, 02/01/13 - 16:16.57 Sº~";

        StringParsing parsing = new StringParsing();
        Properties p = parsing.parseString(s);

        Set<Object> keys = p.keySet();
        Iterator<Object> iter = keys.iterator();

        while(iter.hasNext()) {
            String key = (String) iter.next();
            String value = p.getProperty(key);
            System.out.println(key + " " + value);
        }
    }

}

Edited to add code in response to a comment.

I haven't tested this, since I don't have a file of your data, but here's how you load a Map. The important thing is that you have to create a new Properties object for each Map entry, because the map contains a pointer to the Properties object.

   public void processStrings() {
        Map<String, Properties> dataMap = new TreeMap<String, Properties>(); 
        StringParsing parsing = new StringParsing();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(
                    "dataFile.txt"));
            String line = "";
            while ((line = reader.readLine()) != null) {
                Properties p = parsing.parseString(line);
                dataMap.put(p.getProperty("M:"), p);
            }
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • Thanks. It is more efficient then what I was doing. – Kamran Jun 21 '13 at 10:17
  • I am facing an issue over here. I am now reading different devices, which has different values for M: (MACs), then I store each MAC in a HashMap with a key, then I send this data to graphs, dials,...etc with the corresponding key from the HashMap. The problem is the data mixes, like wrong keys are attached to the data. Any suggestion how can I over come this? Thanks! – Kamran Sep 04 '13 at 06:59
  • You can also see that the code produces Ax later but it appears before in the string. – Kamran Sep 04 '13 at 07:03
  • 1
    @user1109443: Properties in a Properties instance are not guaranteed to be in any order. You ask for a property value by passing the key. As far as your other question, a HashMap should work. – Gilbert Le Blanc Sep 04 '13 at 08:49
  • I tried this method http://stackoverflow.com/questions/1312383/pulling-values-from-a-java-properties-file-in-order but it wont work, I will try with LinkedHashMap as it maintains the order. – Kamran Sep 04 '13 at 10:28
  • I tried LinkedHashMap, but it didn't worked either i replaced the Properties with LinkedHashMap. Can you direct me to some code HashMap approach which you are suggesting, which can be helpful to my scenario. – Kamran Sep 04 '13 at 11:44
  • 1
    @user1109443: I decided a TreeMap would be better, since it would sort by M values. Check out the updated answer. – Gilbert Le Blanc Sep 04 '13 at 13:05