This discussion is archived
7 Replies Latest reply: Dec 28, 2006 1:25 PM by doremifasollatido RSS

multiple substring

807599 Newbie
Currently Being Moderated
hi all ;

im asking for an idea or a little help with a small piece im developing for my course work, im nearlly finishing, but i discovered a little bug before handing date.

i have a txt file which i import data from ,, its generally made of XML tags, its basically a service history for vehicles such as cars and motor bikes, here is an example :

<car><reg>AB04CDE</reg><make>Ford</make><model>Fiesta</model><colour>blue</colour><passenger_no>4</passenger_no></car>

this is not a problem, i have done the extraction of each component like the model and the registeration number and added that to the car list array,

BUT

im trying to add all the parts the have been replaced for each service the car has gone ,, as you can see from the XML code below :

<service><service_no>24892</service_no><reg>K99TRP</reg><date>01:08:2005</date><miles>26000</miles><part_replaced>indicator_bulb</part_replaced><part_replaced>clutch</part_replaced>

the problem im phasing is that i only do extract one part replaced and i dont manage to extract mutilple parts from the same line ,, even using iteration ,, i.e. while loops ;; i have included the following code :
              int part_start_index = line.indexOf("<part_replaced>");
              int part_end_index   = line.indexOf("</part_replaced>");
              partString = line.substring(part_start_index+15,part_end_index);
              part_list.add(new part(partString));
i also tried using a while loop to catch any other parts available in the same line, here is the code :
            int total_part_start_index = line.indexOf("<part_replaced>");
            int total_part_end_index = line.lastIndexOf("<part_replaced>");
            int count = total_part_start_index;
            
            while(count <= total_part_end_index){ 
              int part_start_index = line.indexOf("<part_replaced>");
              int part_end_index   = line.indexOf("</part_replaced>");
              partString = line.substring(part_start_index+15,part_end_index);
              part_list.add(new part(partString));
              count = count + 20;
            }
as you can see from the code, i have tried the lastIndexOf but didnt get anywhere. i wana catch both the (indicator_bulb), (clutch) and others if available on the same line.

i would appreciate any ideas ,, no code required

thank you very much for reading this thread

regards
  • 1. Re: multiple substring
    doremifasollatido Journeyer
    Currently Being Moderated
    I don't think you need lastIndexOf. You need the indexOf that takes a starting point (otherwise, you always start at the beginning of the String). Then, after the first indexOf("<part_replaced>"), start the indexOf("</part_replaced>") at part_start_index+15. (You may want to define a constant PART_START_TAG_LENGTH [or whatever name] instead of 15. Easier to know what the 15 means, and easier to change if the tag changes.) After you get one part, search for the starting tag starting at part_end_index+16. Loop until indexOf returns -1.

    Look in the String API for the two-parameter version of indexOf.

    Or, if you use a readymade XML parser, I think it would give you the list of parts directly, without your having to worry about the above details.
  • 2. Re: multiple substring
    807599 Newbie
    Currently Being Moderated
    thank you very much doremifasollatido

    u saved me :)

    im going to try it now ,, hope it works
  • 3. Re: multiple substring
    doremifasollatido Journeyer
    Currently Being Moderated
    You're welcome. Post again if you have more trouble with this.
  • 4. Re: multiple substring
    807599 Newbie
    Currently Being Moderated
    Dear doremifasollatido ;

    i have typed the code but i have a slight problem , the code only gets one part only , i think i have made a mistake, it doesnt catch the first part from each service line like it did before ,,now it catches only one part and thats it ..

    The code is :
               int startService   = line.indexOf("<service>");
               if (startService > -1) {
                int part_start_tag_length = 15;
                
                int part_start_index = line.indexOf("<part_replaced>");
                int part_end_index   = line.indexOf("</part_replaced>");
                partString = line.substring(part_start_index + part_start_tag_length,part_end_index);
                part_list.add(new part(partString));
                
                System.out.println("first part is "+ partString);
                
                while(part_start_index > -1){
                  part_start_index = line.indexOf("<part_replaced>", part_end_index + 16);
                  part_end_index   = line.indexOf("</part_replaced>");
                  partString = line.substring(part_start_index + part_start_tag_length,part_end_index);
                  part_list.add(new part(partString));
                  System.out.println("other parts are "+ partString);
                }
     
    i have included System.out.println just to check if the parts have been added to the array list ..

    regards
  • 5. Re: multiple substring
    doremifasollatido Journeyer
    Currently Being Moderated
    Your part_end_index within the loop needs to start after the part_start_index within the loop. That is:
    part_end_index   = line.indexOf("</part_replaced>", part_start_index+part_start_tag_length);
    You might as well do that outside the loop, too.

    Better yet, instead of doing a single part outside the 'while' loop, just get the 'part_start_index' outside the loop. Then:
    while (part_start_index != -1)
    {
        //find part_end_index [as above]
        if (part_end_index != -1) // end was found
        {
             // get substring (as you have it)
             // put new part in list (as you have it)
        }
       // as you have it at the beginning of your loop, update the part_start_index with the following:
        part_start_index = line.indexOf("<part_replaced>", part_end_index + 16);              
    }
    The "16" should also be defined as "part_end_tag_length". The two tag lengths should be declared as "final".
  • 6. Re: multiple substring
    807599 Newbie
    Currently Being Moderated
    Dear doremifasollatido ;

    thanks a lot for your help ; words can't describe the amount of appreciation for your help Sir .

    regards
  • 7. Re: multiple substring
    doremifasollatido Journeyer
    Currently Being Moderated
    You're very welcome. Glad I could help. Have a nice day. :)