Sunday 15 June 2014

Efficient string reading in Java? -


I am getting a packet like

"c ~ ¹" M: 0013 A 20040559361 Ax: 48 age: 10 years: 1012 TP: 25b: 45? TM: Wednesday, 02/01/13 - 16: 16.57 SAP ~ "

and other characters that can not be pasted . I will have to read the values ​​after this For example, M:, Ax:, Ay: and store it or drag it into a graph. I know two ways: StreamTokenizer or simple Java argument. The problem is that I tried both But still I get many more errors during reading the values. Tried the ig waste collector, it has not been done.

Below are code for both:

  if (message2.contains ("Ax:")) {Int index = message2.indexOf ("Ax:"); String Ax = message2.substring (index + 3); Chart integration.txt.edonupdate (new milliseconds ()), Double.valueOf (Ax));} StreamTokenizer code: StreamTokanizer ST = New Striener Reader (new string reader (message)); Boolean eof = false; while (st.nextToken ()! = StreamTokenizer.TT_EOF) {int token = st.nextToken (); switch (token) {case streamtokinizer TT_WORD: S Ring words = string. if; (Wordpress ("x")} {Ax = true;} Else E. if (word.contains ("income")) {o = true; } And if (word.contains ("Az")) {edge = true; } And if (word.contains ("P")) {P = true; } And if (word.contains ("B")) {b = true; } And if (word.contains ("Tp")) {TP = true; } Else {Ax = false; Income = wrong; AG = false; P = wrong; B = false; Tp = false; } // System.out.println ("word:" + word); break; Case streamToKonizer TT_NUMBER: double number = st.nval; If (x) {Ax = false; // Errors indicator (XSTR, number); AxStr = number; Sender publishAccelerator ("X:" + String.valueOf (AxStr)); } And if (o) {i = wrong; ICT = number; Sender.publishAccelerator ("AI:" + String.valueOf (AyStr)); } And if (AG) {AG = wrong; AzStr = number; Sender.publishAccelerator ("Az:" + String.valueOf (AzStr)); // System.out.println ("Az:" + Aztr); } And if (p) {p = false; PSRR = number; Sender.publishPressure (String.valueOf (PStr)); //System.out.println("P:"+PStr); } And if (b) {b = wrong; BST = number; Sender.publishBattery (String.valueOf (BSTR)); } And if (TP) {TP = false; TpStr = number; Sender.publishTemp (String.valueOf (TpStr)); } break; Default:}}    

I have put a string parsing program for your text.

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

Here are my results.

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

More Here is the parsing code.

  import java.util iterator; Import java.util.Properties; Import java.util.Set; Public class StringParsing {Private string [] [] Token = {{"Ax:", ""}, {"Ay:", ""}, {"Az:", ""}, {"Tp: "}, {" B: "," "}, {" TM: "," SCE "}}; Public property parseString (string s) {properties p = new property (); {String value = getValue (s, token [ii] [0], token [i] [1]) for (int i = 0; i & lt; tokens.length; i ++); P.setProperty (token [i] [0], value); } Return p; } Private string getValue (string s, string token, string endoton) {int sPos = s.indexOf (token); If (SPS> = 0) {int ePos = s.indexOf (Endoton, SPS + 1); If (ePos> SPS) {sPos + = token.length (); Return s.substring (sPos, ePos); }} Return ""; } Public static zero main (string [] args) {string s = "c ~ ¹¹" m: 0013 a 20040559361 x: 48 age: 10 as: 1012 "+" tp: 25b: 45? TM: Wednesday, 02/01/13 - 16: 16.57 SARI ~ "StringParsing Parsing = New StringParsing (); Properties P = Parsing. PRSstring (S); Set & lt; Object & gt; 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);}}}   

edit to add code in response to a comment.

I have not tested it Yes, because I do not have the file of your data, but here's how you load the map. The important thing is that you have to create a new property object for each map entry, because the map properties for the object Is an indicator.

  Public Zero Processing () {Map & lt; String, Properties & gt; Datamap = New Trimmer & lt; String, Properties & gt; (); StringParsing Persean = New Stringprsing (); {BufferedReader reader = new BufferedReader (new FileReader ( "dataFile.txt")); String line = ""; While ((Line = Reader. Readline ()) = Null {Properties P = P (line) arsing.parseString; DataMap.put (p.getProperty ("M:"), p);} Reader.close (); } Hold (FileNotFoundException e) {e.printStackTrace ();} Hold (IOException e) {e.printStackTrace ();}}    

No comments:

Post a Comment