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,
you have to use Patterns/Regular expressions, and see in your while if the token contains only alpha characterssee Pattern.matches()
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.
> 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...
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
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...
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?
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...
What is your required output?
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"
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);
}
Hi Thanks... its working but there is no space between (A Beautiful Day)its coming like (ABeautifulDay)..What should i be doing..
replace buffer += token with buffer += " "+token, or something like that...i think you can manage to think by yourself, nope?
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.
> 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..
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.
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.