## Algorithms

Moderators: None (Apply to moderate this forum)
Number of posts: 786

This Forum Only

Interesting problem Posted by ilarum on 25 Aug 2003 at 7:33 AM
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);

}
Re: Interesting problem Posted by martijn82 on 5 Jan 2004 at 4:32 AM
: 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);
}
}

Re: Interesting problem Posted by arashpartow on 11 Apr 2004 at 12:55 AM
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);
:
:
: }
:

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic