HI Please Help With Parsing Of String:

Hi all,

I am trying to parse a String :

Say my String is :

String input :"0 0 A 0 0 BC / Day 5.23 6.45 M (A Beautiful Day) 0 0 F";

I am attaching my code I am getting a output like this :

import java.util.StringTokenizer;

publicclass TrialParser

{

static String input;

publicstaticvoid main(String[] args)

{

input ="0 0 A 0 0 BC / Day 5.23 6.45 M (A Beautiful Day) 0 0 F";

StringTokenizer st =new StringTokenizer(input);

while (st.hasMoreElements())

System.out.println(st.nextToken());

}

}

OUTPUT I AM GETTING IS:

0

0

A

0

0

BC

/

Day

5.23

6.45

M

(A

Beautiful

Day)

0

0

F

But I want my OUTPUT TO BE THIS .... JUST THIS:

A

BC

M

F

what should I be doing, Can I have multiple tokens,

[1383 byte] By [New_to_Javaa] at [2007-11-27 2:48:04]
# 1
you have to use Patterns/Regular expressions, and see in your while if the token contains only alpha characterssee Pattern.matches()
calvino_inda at 2007-7-12 3:17:59 > top of Java-index,Java Essentials,Java Programming...
# 2
You can try the following instead of regex StringTokenizer st = new StringTokenizer(input, "0"); Only 0s will be eliminated and the remaining will be printed on the console.
AnanSmritia at 2007-7-12 3:17:59 > top of Java-index,Java Essentials,Java Programming...
# 3

> You can try the following instead of regex

>

> StringTokenizer st = new

> StringTokenizer(input, "0");

>

> Only 0s will be eliminated and the remaining will be

> printed on the console.

Thanks, but...

I don't think i can use that , because later i want to collect the 0 's or any integer as they are coordinates...

and the other thing is i don't know much about pattern matching .. i will give a try.. lets see...

in the meantime if you can come up with something which i can help me .. i Would appericate that...

thanks again...

New_to_Javaa at 2007-7-12 3:17:59 > top of Java-index,Java Essentials,Java Programming...
# 4

since data you want to retrieve matches the regular expression "any line that contains only alpha uppercase characters", this should be something like:

if (Pattern.matches("[A-Z]*", token)

not sure about the " * " in reg exp, but i think it means "the token can have as many chars as he wants" , and "[A-Z]" means each character should be between A and Z

just try

calvino_inda at 2007-7-12 3:17:59 > top of Java-index,Java Essentials,Java Programming...
# 5

Hi all,

can I get an out put like this :

0

0

A

0

0

BC

/ Day

5.23

6.45

M

(A Beautiful Day)

0

0

F

I just realized i hsould not be using StringTokenizer,

But can I do do it without StringTokenizer, I should use just iterate by checking the next element thing, i don't know much about this,,,

Please Help...

New_to_Javaa at 2007-7-12 3:17:59 > top of Java-index,Java Essentials,Java Programming...
# 6

you said:

OUTPUT I AM GETTING IS:

0

0

A

0

0

BC

/

Day

5.23

6.45

M

(A

Beautiful

Day)

0

0

F

But I want my OUTPUT TO BE THIS .... JUST THIS:

A

BC

M

F

and then you said:

Hi all,

can I get an out put like this :

0

0

A

0

0

BC

/ Day

5.23

6.45

M

(A Beautiful Day)

0

0

F

Are you really sure of what you want to do?

calvino_inda at 2007-7-12 3:17:59 > top of Java-index,Java Essentials,Java Programming...
# 7
Now i am clear, initally the output i thought would be fine actually doesn't make much of sense while reading again...I am Sorry ...But Can i iterate character by charcter... Is it possible... what should I be doing to achieve the output...
New_to_Javaa at 2007-7-12 3:17:59 > top of Java-index,Java Essentials,Java Programming...
# 8
What is your required output?
AnanSmritia at 2007-7-12 3:17:59 > top of Java-index,Java Essentials,Java Programming...
# 9
Hi,I Want to have the output like this... 00A00BC/Day5.236.45M(A Beautiful Day)00FFrom the input:"0 0 A 0 0 BC /Day 5.23 6.45 M (A Beautiful Day) 0 0 F"
New_to_Javaa at 2007-7-12 3:17:59 > top of Java-index,Java Essentials,Java Programming...
# 10

StringTokenizer stok = new StringTokenizer(input);

String token; boolean par = false; String buffer = "";

while (stok.hasMoreTokens()) {

token = stok.nextToken();

if (token.startsWith("("))

par = true;

if (token.endsWith(")") {

par = false;

System.out.println(buffer);

buffer = "";

}

if (par)

buffer += token;

else

System.out.println(token);

}

calvino_inda at 2007-7-12 3:17:59 > top of Java-index,Java Essentials,Java Programming...
# 11
Hi Thanks... its working but there is no space between (A Beautiful Day)its coming like (ABeautifulDay)..What should i be doing..
New_to_Javaa at 2007-7-12 3:17:59 > top of Java-index,Java Essentials,Java Programming...
# 12
replace buffer += token with buffer += " "+token, or something like that...i think you can manage to think by yourself, nope?
calvino_inda at 2007-7-12 3:17:59 > top of Java-index,Java Essentials,Java Programming...
# 13
I think you're better off using a regular expression that matches your input. You can then use capturing groups to get only the elements you want from it.
Herko_ter_Horsta at 2007-7-12 3:17:59 > top of Java-index,Java Essentials,Java Programming...
# 14

> replace buffer += token with buffer += " "+token, or

> something like that...

>

> i think you can manage to think by yourself, nope?

Thanks , i was trying that, but just now i tried:

> replace buffer += token with buffer += " "+token,

buffer += token + " ";

and it works fine... thanks a lot..

New_to_Javaa at 2007-7-12 3:17:59 > top of Java-index,Java Essentials,Java Programming...
# 15

Hi Again,

I was trying all kind of inputs, Now I am stuck again, Please help..

I want to have a input as this :

Input:

"0 0 A 0 0 BC /Day 5.23 6.45 M (A Beautiful Day) 0 0 F 0.0011 Km 0.0005 Gr [(Some Text:)19.1(V1.CR1.KM1 Copyright )19.6(T)69.1(Teslla 2001)]TJ ET"

Required Output:

0

0

A

0

0

BC

/Day

5.23

6.45

M

(A Beautiful Day)

0

0

F

0.0011

Km

0.0005

Gr

[(Some Text:)19.1(V1.CR1.KM1 Copyright )19.6(T)69.1(Teslla 2001)]

TJ

ET

I not getting the logic , Please help.

New_to_Javaa at 2007-7-21 20:33:11 > top of Java-index,Java Essentials,Java Programming...
# 16

Hi Again,

I was trying all kind of inputs, Now I am stuck again, Please help..

I want to have a input as this :

Input:

"0 0 A 0 0 BC /Day 5.23 6.45 M (A Beautiful Day) 0 0 F 0.0011 Km 0.0005 Gr [(Some Text:)19.1(V1.CR1.KM1 Copyright )19.6(T)69.1(Teslla 2001)]TJ ET"

Required Output:

0

0

A

0

0

BC

/Day

5.23

6.45

M

(A Beautiful Day)

0

0

F

0.0011

Km

0.0005

Gr

[(Some Text:)19.1(V1.CR1.KM1 Copyright )19.6(T)69.1(Teslla 2001)]

TJ

ET

I not getting the logic , Please help.

New_to_Javaa at 2007-7-21 20:33:11 > top of Java-index,Java Essentials,Java Programming...