New To Java - Parsing Strings

How would I parse a String like this :

"CW5188>APRS,TCPXX*,qAX,T2WXCWOP2:@300244z3506.97N/11826.90W_281/007g009t051r000P000p000h16b10052v8"

into something like this :

String call ="CW5188";

String Mode ="APRS,TCPXX*,qAX,";

String location ="300244z3506.97N/11826.90W"

etc....

?

[633 byte] By [biojaea] at [2007-11-26 23:17:18]
# 1
What are the rules?
DrLaszloJamfa at 2007-7-10 14:18:32 > top of Java-index,Java Essentials,New To Java...
# 2
How would you do it "by hand"? Until you can show us how you would interpret that string, there's no way for us to tell you how to program it.G
ggaineya at 2007-7-10 14:18:32 > top of Java-index,Java Essentials,New To Java...
# 3
What are the rules?"CW5188>APRS,TCPXX*,qAX,T2WXCWOP2:@300244z3506.97N/11826.90W_281/007g009t051r000P000p000h16b10052v8"%s%s%f%f%d%d%d%d%d%d%d%d%d%d%s = string %d = int %f = float
biojaea at 2007-7-10 14:18:32 > top of Java-index,Java Essentials,New To Java...
# 4

Those aren't rules, those are hints - I know I can't figure out what you want to do.

Try again - given your example string, tell us what maps to each of your %x entries. And how do you know when something is a delimiter and when its part of the next/previous entry?

If you can't describe the problem well enough for a random human to figure out what you're trying to accomplish, you certainly won't be able to write code for it.

Grant

ggaineya at 2007-7-10 14:18:32 > top of Java-index,Java Essentials,New To Java...
# 5

How would I parse a String like this :

"CW5188>APRS,TCPXX*,qAX,T2WXCWOP2:@300244z

3506.97N/11826.90W_281/007g009t051r000P000p000h16b1005

2v8"

into something like this :

String call = "CW5188";

String Mode = "APRS,TCPXX*,qAX,";

String location = "300244z3506.97N/11826.90W"

etc....

?

It looks like your rules MIGHT be:

call is everything before the '>'

Mode is the next three strings (separated by commas)

Location is everything between the '@' and '_'

Would that be correct?

CSAngela at 2007-7-10 14:18:32 > top of Java-index,Java Essentials,New To Java...
# 6
It looks like your rules MIGHT be:I don't know about you, but I didn't learn Java to become a dentist and pull teeth.If the original poster can't be clear, why should anyone even bother to help?
DrLaszloJamfa at 2007-7-10 14:18:32 > top of Java-index,Java Essentials,New To Java...
# 7

It looks like your rules MIGHT be:

I don't know about you, but I didn't learn Java to

become a dentist and pull teeth.

If the original poster can't be clear, why should

anyone even bother to help?

:-) Having spent many years interfacing with clients who have a very clear understanding of what they want but cannot even make a start at expressing what they want I think I am well qualified to be a dentist.

sabre150a at 2007-7-10 14:18:32 > top of Java-index,Java Essentials,New To Java...
# 8

It looks like your rules MIGHT be:

I don't know about you, but I didn't learn Java to

become a dentist and pull teeth.

If the original poster can't be clear, why should

anyone even bother to help?

The @OP was clear - they wanted to know how to parse the string. They even gave the results of the parse. Can't get much clearer than that. ;-)

Sometimes people just need to be pointed in the right direction.

CSAngela at 2007-7-10 14:18:32 > top of Java-index,Java Essentials,New To Java...
# 9

What are the rules?

"CW5188>APRS,TCPXX*,qAX,T2WXCWOP2:@300244z3506.97N/118

26.90W_281/007g009t051r000P000p000h16b10052v8"

%s%s%f%f%d%d%d%d%d%d%d%d%d%d

%s = string

%d = int

%f = float

the delimeters would be : (Space-Seperated)

> , : @ z g t r P h b v

biojaea at 2007-7-10 14:18:32 > top of Java-index,Java Essentials,New To Java...
# 10
Stringtokenizer?String.split("&"); ?
prigasa at 2007-7-10 14:18:32 > top of Java-index,Java Essentials,New To Java...
# 11
The string split method is very helpful for parsing. Give it a shot.
CSAngela at 2007-7-10 14:18:32 > top of Java-index,Java Essentials,New To Java...
# 12

It looks like your rules MIGHT be:

I don't know about you, but I didn't learn Java to

become a dentist and pull teeth.

If the original poster can't be clear, why should

anyone even bother to help?

It's Friday, so it is a way to pass the time until the beer arrives?

jschella at 2007-7-10 14:18:32 > top of Java-index,Java Essentials,New To Java...
# 13

It looks like your rules MIGHT be:

There is no common delimiter in the String and even if i was a CSV file then conformation would still be required in most professional environments. Only a smart as would rush in.

_helloWorld_a at 2007-7-10 14:18:32 > top of Java-index,Java Essentials,New To Java...
# 14

Never mind, I found A way how to parse it

import java.util.*;

public class SampleStringParser

{

char[] charAry;

public SampleStringParser(String toParse)

{

charAry = new char[toParse.length()];

char[] characters = toParse.toCharArray();

for(int i = 0; i < characters.length; i++)

{

charAry<i> = characters<i>;

}

}

public String parse(String s)

{

String parseable = new String(charAry);

int index = parseable.indexOf(s);

if(index == -1) return null;

char[] temp = charAry;

charAry = new char[temp.length- index];

for(int i = index + 1, b =0; i < temp.length; i++, b++)

{

charAry<b> = temp<i>;

}

return parseable.substring(0 ,index);

}

public static String[] APRSDataParse(String APRS)

{

SampleStringParser ssp =

new SampleStringParser(APRS);

String Tokens = ssp.parse(">") + "\n" +

ssp.parse(",") + "\n" +

ssp.parse(",") + "\n" +

ssp.parse(",") + "\n" +

ssp.parse("@") + "\n" +

ssp.parse("z") + "\n" +

ssp.parse("/") + "\n" +

ssp.parse("W") + "\n" +

ssp.parse("/") + "\n" +

ssp.parse("g") + "\n" +

ssp.parse("t") + "\n" +

ssp.parse("r") + "\n" +

ssp.parse("P") + "\n" +

ssp.parse("p") + "\n" +

ssp.parse("h") + "\n" +

ssp.parse("b") + "\n" +

ssp.parse("v");

System.out.println("");

StringTokenizer token = new StringTokenizer(Tokens, "\n", false);

String[] stringAry = new String[token.countTokens()];

for(int i = 0;token.hasMoreTokens(); i++)

{

stringAry<i> = token.nextToken();

System.out.println(stringAry<i>);

}

String[] temp = stringAry;

stringAry = new String[temp.length-3];

System.out.println("");

stringAry[0] = temp[0];

System.out.println(stringAry[0]);

for(int i = 5, b =1; i < temp.length; i++, b++)

{

stringAry<b> = temp<i>;

System.out.println(stringAry<b>);

}

ArrayList<String> aryList = new ArrayList<String>();

for(int i = 0; i < stringAry.length; i++)

{

aryList.add(stringAry<i>);

}

stringAry = aryList.toArray(new String[0]);

return stringAry;

}

}

biojaea at 2007-7-10 14:18:32 > top of Java-index,Java Essentials,New To Java...