Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Interesting problem

ilarumilarum Posts: 28Member
Hi,
Assume that I have a String msg = "ABC:DEF:GHIJ:KLMN"; Now I need to parse this based on the delimiter (':' in this case). such a way that I store these values as name-value in a hashtable.

I did write the code. But the hitch is at one last stage there is an excpetion when we are left with KLMN.
I wrote the code in java as follows

String concat = "ABC:DEF:GHIJ:KLMN:OPQ:RST";
int length = concat.length();
int j;
for(; length > 0 ;)
{

j = concat.indexOf(':');
String key = concat.substring(0, j);
System.out.println("Key " + key);
concat = concat.substring(j + 1, length);
System.out.println("Concat " + concat);
length = concat.length();
System.out.println("length " + length);
j = concat.indexOf(':');
String value = concat.substring(0, j);
System.out.println("Value " + value);
concat = concat.substring(j + 1, length);
System.out.println("Concat " + concat);
length = concat.length();
System.out.println("length " + length);


}

Comments

  • martijn82martijn82 Posts: 3Member
    : Hi,
    : Assume that I have a String msg = "ABC:DEF:GHIJ:KLMN"; Now I need to parse this based on the delimiter (':' in this case). such a way that I store these values as name-value in a hashtable.
    :
    : I did write the code. But the hitch is at one last stage there is an excpetion when we are left with KLMN.
    : I wrote the code in java as follows
    :
    : String concat = "ABC:DEF:GHIJ:KLMN:OPQ:RST";
    : int length = concat.length();
    : int j;
    : for(; length > 0 ;)
    : {
    :
    : j = concat.indexOf(':');
    : String key = concat.substring(0, j);
    : System.out.println("Key " + key);
    : concat = concat.substring(j + 1, length);
    : System.out.println("Concat " + concat);
    : length = concat.length();
    : System.out.println("length " + length);
    : j = concat.indexOf(':');
    : String value = concat.substring(0, j);
    : System.out.println("Value " + value);
    : concat = concat.substring(j + 1, length);
    : System.out.println("Concat " + concat);
    : length = concat.length();
    : System.out.println("length " + length);
    :
    :
    : }
    :


    First of all I suggest you use while( 0 < length ) instead of the for loop.

    The problem is that you assume that the string ends on a :. You read the last key (OPQ) till the :, but after that you try to find the corresponding value by finding the next :, while there is no : anymore after the RST.
    Also the code assumes the syntax of your string is correct.
    Im not sure, but I assume that indexOf() returns 1 if the character is not found. The code would look something like this:

    String concat = "ABC:DEF:GHIJ:KLMN:OPQ:RST";
    int length = concat.length();
    int j;
    while( 0 < length ) {

    j = concat.indexOf(':');
    String key = concat.substring(0, j);
    System.out.println("Key " + key);
    concat = concat.substring(j + 1, length);
    System.out.println("Concat " + concat);
    length = concat.length();

    System.out.println("length " + length);
    j = concat.indexOf(':');
    If (j == -1) { // The : delimiter is not found, assume end of line.
    J = concat.length() 1;
    }
    String value = concat.substring(0, j);
    System.out.println("Value " + value);

    if ( j < (length 1) ) { // Only if the line is not finished.
    concat = concat.substring(j + 1, length);
    System.out.println("Concat " + concat);
    length = concat.length();
    System.out.println("length " + length);
    }
    }




  • arashpartowarashpartow Posts: 2Member
    Hi,

    Have you heard of the Java StringTokenizer class ?
    Its in the SDK.



    Arash Partow

    __________________________________________________
    Be one who knows what they don't know,
    Instead of being one who knows not what they don't know,
    Thinking they know everything about all things.
    http://www.partow.net




    : Hi,
    : Assume that I have a String msg = "ABC:DEF:GHIJ:KLMN"; Now I need to parse this based on the delimiter (':' in this case). such a way that I store these values as name-value in a hashtable.
    :
    : I did write the code. But the hitch is at one last stage there is an excpetion when we are left with KLMN.
    : I wrote the code in java as follows
    :
    : String concat = "ABC:DEF:GHIJ:KLMN:OPQ:RST";
    : int length = concat.length();
    : int j;
    : for(; length > 0 ;)
    : {
    :
    : j = concat.indexOf(':');
    : String key = concat.substring(0, j);
    : System.out.println("Key " + key);
    : concat = concat.substring(j + 1, length);
    : System.out.println("Concat " + concat);
    : length = concat.length();
    : System.out.println("length " + length);
    : j = concat.indexOf(':');
    : String value = concat.substring(0, j);
    : System.out.println("Value " + value);
    : concat = concat.substring(j + 1, length);
    : System.out.println("Concat " + concat);
    : length = concat.length();
    : System.out.println("length " + length);
    :
    :
    : }
    :

Sign In or Register to comment.