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.    « Previous | 1 | 2 |

Posts:556
Registered: 12/16/98
Re: Parsing string  
Jul 14, 2004 6:45 AM (reply 15 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.

Sorry, misread the problem. Go with StringTokenizer like everyone has said, however I would store the values in an ArrayList().
 

Posts:21,719
Registered: 98-02-20
Re: Parsing string  
Jul 16, 2004 7:35 AM (reply 16 of 19)



 
You can use a java.util.LinkedHashMap. That will preserve the keys in the insertion order.

The only way to deal with duplicates properly is to have a List as the value. When you want to add a new duplicate, you'll have to get the List value using the key and add the new duplicate to the List.

This forces you to use a List even if there's just one value, which can be awkward. Might be a good time to write a class that uses a LinkedHashMap and publishes methods that make dealing with non-duplicate values easier.

The StringTokenizer and looping over the tokens idea is the right way to go, IMO.

I'd encapsulate all of this into a class. The HttpRequest idea is the right path. Hide the parsing and access details in a class of your own design.

%
 

Posts:18,384
Registered: 21.03.00
Re: Parsing string  
Jul 16, 2004 7:50 AM (reply 17 of 19)



 
Hi,

I would have to say that the other guys are solving the problem in very strange ways... In my humble opinion I would say that this is a good aproach if you are using jdk1.4 or later.

public class Test {     public static final String DELIMITER = ":";    public static final String TO_LOOK_FOR = "student"public static void main(String[] args) {         String lecture = "computing:uni=unknown:teacher=smith:student=alex:grade=a:time=full:student=john:grade=b:time=part:";        String[] parts = lecture.split(DELIMITER);        StringBuffer result = new StringBuffer();        int students = 0;        for (int i = 0; i < parts.length; i++) {            if (parts[i].startsWith(TO_LOOK_FOR)) {                result.append(TO_LOOK_FOR);                result.append(++students);                result.append(parts[i].substring(TO_LOOK_FOR.length()));            }            else {                result.append(parts[i]);            }            result.append(DELIMITER);        }         System.out.println("End result is " + result.toString());    } }


/Kaj
 

Posts:21,719
Registered: 98-02-20
Re: Parsing string  
Jul 16, 2004 10:37 AM (reply 18 of 19)



 

There's always a simple and wrong answer.

I think a good solution demands more than a StringBuffer. What if he wants to know the grade and status of a student?

You really need a few classes here: Student (with name, status data), Course (with title, section data), a relationship that associates a Student with a Course and maintains grade data, etc. Your parser ought to be a factory that will parse that string and spit all these classes out, IMO.

%
 

Posts:18,384
Registered: 21.03.00
Re: Parsing string  
Jul 16, 2004 10:50 AM (reply 19 of 19)



 
If you look at the question you will see that he is not interested in reading the values. He just wants to convert the String to another format. The reason for that might be to be able to forward it to an existing class that can parse it.

I think it is wrong of us to make an asumtion that he wants to know the grade of a student. In that case we can also make the asumption what he wants to be able to export the data to excel and show some fancy graphs. But that is not the question.

/Kaj
 
This topic has 19 replies on 2 pages.    « Previous | 1 | 2 |