I have a string and from this string, I want to get password file path which is identified by an option (-sn).
String s = "msqlsum81pv 0 0 25 25 25 2 -sn D:\workdir\PV_81\config\sum81pv.pwf -C 5000"
above line is a configuration line which can be with either -sn or -n.
please suggest how to get D:\workdir\PV_81\config\sum81pv.pwf line from above string or the string may be with quoted string.
below is my code which check only -sn option but I want to check with either -sn or -n .
if ( s.matches( "^\\s*msql.*$" ) )
{
StringTokenizer st = new StringTokenizer( s, " " );
while ( st.hasMoreTokens() )
{
if ( st.nextToken().equals( "-sn" ) )
{
pwf = st.nextToken();
}
}
}
I want to use StreamTokenizer instead of StringTokenizer class and get D:\workdir\PV_81\config\sum81pv.pwf
this path may be containing spaces in it.
String s = "msqlsum81pv 0 0 25 25 25 2 -sn D:\workdir\PV_81\config\sum81pv.pwf -C 5000"
if ( s.matches( "^\\s*msql.*$" ) )
{
StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(s));
while (tokenizer.nextToken() != StreamTokenizer.TT_EOF)
{
System.out.println(tokenizer.sval);
}
}
You should use a regular expression to detect that option in a more general way. If you want a quick fix you can use the OR operator in your if but each time that new operations appear your if will grow and it's a bad idea.
if ( s.matches( "^\\s*msql.*$" ) )
{
StringTokenizer st = new StringTokenizer( s, " " );
while ( st.hasMoreTokens() )
{
string token = st.nextToken();
if ( token.equals( "-sn" ) || token.equals("-n" ) )
{
pwf = st.nextToken();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With