CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2015
    Posts
    1

    Post How to write a code Using while loop begin and extend untill you find end in Java?

    -12

    down vote

    favorite





    I have to write a while Loop for the a2l file below
    if(line.startsWith("/begin")) {
    new tokens
    ->while

    read until /end with same type
    } else {
    ignore:
    }

    I am also attaching a2l Parser code
    /begin CHARACTERISTIC LS_Right_Green_DiagValue ""
    VALUE 0x1032E __UBYTE_S 0 NO_COMPU_METHOD 0 255
    ECU_ADDRESS_EXTENSION 0x0
    EXTENDED_LIMITS 0 255
    FORMAT "%.15"
    /begin IF_DATA CANAPE_EXT
    100
    LINK_MAP "IK_ucLSRG_DiagValue" 0x1032E 0x0 0 0x0 1 0x87 0x0
    DISPLAY 0 0 255
    /end IF_DATA
    SYMBOL_LINK "IK_ucLSRG_DiagValue" 0
    /end CHARACTERISTIC

  2. #2
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: How to write a code Using while loop begin and extend untill you find end in Java

    This sounds like homework, so without wanting to give too much away, the best place to start is to ask yourself, how you would find a single "/begin", "/end" block. I expect the logic would be something along the lines of

    1) Create a file reader
    2) Create a method called readBlock(takes the file reader as a parameter)
    //The rest of the steps are in the readBlock(reader) method
    3) create a container of lines (e.g List<String> blockLines = new LinkedList<String>() )
    4) read a line from the file reader,
    5) if the line starts with "/begin" read the next line and then go to step 6, else read the next line and repeat step 5)
    6) if the line does not start with "/end" then add the line to blockLines, read the next line and repeat step 6), else the block is now complete
    7) print the contents of blockLines

    To find nested blocks you can use recursion (a slight modification of the above)

    1) Create a file reader
    2) Create a method called readBlock(takes the file reader as a parameter)
    3) Inside the method create a container of lines (e.g List<String> blockLines = new LinkedList<String>() )
    4) read a line from the file reader,
    5) if the line starts with "/begin" read the next line and then go to step 6, else read the next line and repeat step 5)
    6) if the line starts with "/begin", call readBlock(reader), else if the line does not start with "/end" then add the line to blockLines, read the next line and repeat step 6), else the block is now complete
    7) print the contents of blockLines

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured