Hi All, I am writing a program. Basically I got a string which contains keys and values, for example "key1=AAA key2=BBB key3=CCC" The easiest way for me to get the key and the value is by writing this string into a property file and then read the property file back using "ResourceBundle.getClass(property file)" My question: Is there other way to parse the string directly to get the key and the value using the similar concept as the ResourceBundle.getBundle? Can you please let me know what is the API that I need to use? Greatly Appreciated for your help! Thank you, -David Sundo
You can do all the work yourself for good hands on experience.
How to code that? For the text file, see the JDK doc for how you'd make a FileReader with a filename, and a BufferedReader with the FileReader. For the parse, look at String.split(). And for the storage bit, see my Collections Crib Sheet, then the JDK for maybe HashMap. Lemme know if that helps!
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Jul 02, 2003
Thanks Stan for the replied! Actually, I do not want to save any things in the properties file. Would it possible to parse the string (the string is "key1=AAA key2=BBB") directly based on the key and value? If I use the hashtable, then it looks like that I need to parse the key and value then put them into the table which is a kinda a little bit coding. Thank you.
If the format is consistant, you could use the split() member of String.
Of course, this relies on the input string being well-formatted (and on there being no whitespace in either the keys or the values), but you get the picture. Explination of Regular expression used: [\\s=] The  matches any of the enclosed elements in an "or" manner. \\s - the two backslashes are interpolated by the compiler to mean to place a literal back slash in the string (So the string actually reads: "[\s=]" \s : regular expression matching any white space = : matches the '=' character So what we have is a regular expression that matches either whitespace or the '=' character and uses those to delimit the string (via the "split" method). Futher explainations of regular expressions can be found here, and of course other places on the web.
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.