Home arrow static arrow Java Programming [Archive] - Parsing string
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Parsing string
This topic has 19 replies on 2 pages.    1 | 2 | Next »

Posts:6
Registered: 7/13/04
Parsing string  
Jul 13, 2004 6:10 AM



 
Hi,

I have a String which has a value as such:

"lecture=computing:uni=unknown:teacher=smith:student=alex:grade=a:time=full:student=john:grade=b:time=part:"

I want to change this string so that i can put a number in front of the keys which occur more than once.
For example I have two students so i want to create a new string which looks like this:

lecture=computing:uni=unknown:teacher=smith:student1=alex:grade1=a:time1=full:student2=john:grade2=b:time2=part:

and so on depending upon how many times student occurs.

Does anybody have any ideas on how to do this?

Thanks
Alex
 

Posts:556
Registered: 12/16/98
Re: Parsing string  
Jul 13, 2004 6:28 AM (reply 1 of 19)



 
"lecture=computing:uni=unknown:teacher=smith:student=alex:grade=a:time=full:student=john:grade=b:time=part:"

String[] student= req.getParameterValues("student");if (student== null || student[0].trim().equals("")) {     student= new String[1];     student[0] = "none";} student[0] represents one studentstudent[1] represents another.......


Traverse through the student array and get the values. Then build the url string you want.
 

Posts:192
Registered: 7/21/03
Re: Parsing string  
Jul 13, 2004 6:53 AM (reply 2 of 19)



 
best way to do these parsings (especially nested parsings) is to make a hash table and use Finiti State Machine Approach (looping around if elses):

in this case in the hash table make an object called student which will have 3 fields : student, grade ...

and just have a flag how many times it occurs. When you are done just output a string using the hashtable?

 

Posts:128
Registered: 06/06/03
Re: Parsing string  
Jul 13, 2004 6:53 AM (reply 3 of 19)



 
I don't think the solution of jmennem will work, it's not a querystring that he's talking about, therefore there is no HttpServletRequest object. For me, the solution is to use StringTokenizer to count the number of times the string "student=" appears. If the main string doesn't start by "student=" all the odd positions of the array will have the String containing the value to be replaced.
You'll have to look for the first occurrence of ":" .

Hope it helps,

Carlos Ferreira
 

Posts:7
Registered: 7/13/04
Re: Parsing string  
Jul 13, 2004 6:59 AM (reply 4 of 19)



 
try this (you should add some code for parsing the last pair of key-value, or just add ":" in the end of the string, and then delete it in the output string)
public class Parser {     public static void main(String[] args) {        StringBuffer str = new StringBuffer(                "lecture=computing:uni=unknown:teacher=smith:student=alex:grade=a:time=full:student=john:grade=b:time=part");        StringBuffer new_str = new StringBuffer();        Hashtable ht = new Hashtable();        ht.put(new String("student"), new Integer(0));        ht.put(new String("grade"), new Integer(0));        ht.put(new String("time"), new Integer(0));         while (str.indexOf(":") > 0) {            String pair = str.substring(0, str.indexOf(":"));            String key = pair.substring(0, pair.indexOf("="));            String value = pair.substring(pair.indexOf("=") + 1, pair.length());            str.delete(0, str.indexOf(":") + 1);             if (ht.containsKey(key)) {                int i = ((Integer) ht.get(key)).intValue();                ht.put(key, new Integer(++i));                new_str.append(key + i + "=" + value + ":");            } else {                new_str.append(pair + ":");            }        }         System.out.println(new_str);    }}
 

Posts:6
Registered: 7/13/04
Re: Parsing string  
Jul 13, 2004 7:10 AM (reply 5 of 19)



 
I agree with using string tokenizer, and using ":" as the delimeter. which will return me all the individual values

lecture=computing
uni=unknown
student=alex
grade=a
time=full
student=smith
grade=b
time=part

the above is just an example and student could have many more elements within it. After getting these individual strings how would i loop through and assign student1 and student2 etc.
 

Posts:128
Registered: 06/06/03
Re: Parsing string  
Jul 13, 2004 7:23 AM (reply 6 of 19)



 
Just iterate the array returned by string tokenizer, create int variables for each of the keys and every iteration, you find the position of "=" and concatenate the corresponding int value and then you increment the same value.

Carlos Ferreira
 

Posts:6
Registered: 7/13/04
Re: Parsing string  
Jul 13, 2004 7:31 AM (reply 7 of 19)



 
Can you expand on this please.

Thanks
 

Posts:128
Registered: 06/06/03
Re: Parsing string  
Jul 13, 2004 7:41 AM (reply 8 of 19)



 
For instance:
String[] values = {"students", "grades","...."};int students = 0;int grades = 0;....StringTokenizer st .... while( st.hasMoreTokens() ){  for( int j = 0; j < values.length; j++ )  {    if( st.nextValue().startWith(values[j])      // Here you split the string in the "=" signal and add the corresponding int value, and concatenate       // back the strings      // afterwards, you increment the int value. The code to do this i leave to you  }} 
 

Posts:6
Registered: 7/13/04
Re: Parsing string  
Jul 13, 2004 9:20 AM (reply 9 of 19)



 
Thanks for the reply guys...

I opted for a hashtable solution adding all the values to the hashtable then incrementing the duplicate keys as required. The problem now is that the keys do not come back in the order that i put them into the hashtable.

Does anyone know how to get the keys out in the order they went in?

I tried using a second hashtable which holds a int as a key and a hashtable holding the key value pair as the value but then i cant do the check for duplicates.
 

Posts:192
Registered: 7/21/03
Re: Parsing string  
Jul 13, 2004 9:37 AM (reply 10 of 19)



 
no,
in hash table you dont have to put keys sequentially. You can put them whereever you want.

e.g. a godo indexing approach would be to
currentTime% <some large number> = small integer key
 

Posts:6
Registered: 7/13/04
Re: Parsing string  
Jul 13, 2004 9:40 AM (reply 11 of 19)



 
Unfortunately that answer does not help me!
 

Posts:192
Registered: 7/21/03
Re: Parsing string  
Jul 13, 2004 11:32 AM (reply 12 of 19)



 
hopefully, you know how a hashtable works. It has a key and value field. The keys, while storing donot have to be sequentia. You could come up with your own way to store them, so that when you retrieve them you get them in time synchronous manner. Hopefully, this should make sense.
 

Posts:192
Registered: 7/21/03
Re: Parsing string  
Jul 13, 2004 11:32 AM (reply 13 of 19)



 
if you dont still understand, try researchingon the "chubaka defense" .
 

Posts:123
Registered: 5/24/04
Re: Parsing string  
Jul 13, 2004 12:05 PM (reply 14 of 19)



 
Why not use another format?
 
This topic has 19 replies on 2 pages.    1 | 2 | Next »