how to compare Strings

Let said we have String s = " I went to the beach"

and the user input String input = " He went to the mall"

If any word in a input's sentence match the words in string "s", it will display "matched".

For this example, "went", "to", "the" of input sentence were found in string s, so will print "matched"

I know that I have to use StringTokenizer to do it.

For example:

StringTokenizer token = new StringTokenizer(s );

StringTokenizer inputToken = new StringTokenizer(input);

String nextword = token.nextToken();

String nextInput = inputToken.nextToken();

To compare I put like this:

if(nextword.equalsIgnoreCase(nextInput))

System.out.println("Matched");

but I couldn't get it to work.

Can anyone help me ?

[806 byte] By [chaohiolabonjoura] at [2007-9-29 14:32:17]
# 1

Hi

Try this segment

String s = "I went to the mall";

String input = "He went to the mall";

StringTokenizer token = new StringTokenizer(s );

StringTokenizer inputToken = new StringTokenizer(input);

String nextword="";

String nextInput="";

while (token.hasMoreTokens())

{

nextword = token.nextToken();

System.out.println(nextword);

nextInput = inputToken.nextToken();

System.out.println(nextInput);

//To compare I put like this:

if(nextword.equalsIgnoreCase(nextInput))

System.out.println("Matched");

}

-GK

Kavi_herea at 2007-7-15 5:18:13 > top of Java-index,Other Topics,Algorithms...
# 2

Hi

Try this segment

String s = "I went to the mall";

String input = "He went to the mall";

StringTokenizer token = new StringTokenizer(s );

StringTokenizer inputToken = new StringTokenizer(input);

String nextword="";

String nextInput="";

while (token.hasMoreTokens())

{

nextword = token.nextToken();

System.out.println(nextword);

nextInput = inputToken.nextToken();

System.out.println(nextInput);

//To compare I put like this:

if(nextword.equalsIgnoreCase(nextInput))

System.out.println("Matched");

}

-GK

Kavi_herea at 2007-7-15 5:18:13 > top of Java-index,Other Topics,Algorithms...
# 3

Hi

Try this segment

String s = "I went to the mall";

String input = "He went to the mall";

StringTokenizer token = new StringTokenizer(s );

StringTokenizer inputToken = new StringTokenizer(input);

String nextword="";

String nextInput="";

while (token.hasMoreTokens())

{

nextword = token.nextToken();

System.out.println(nextword);

nextInput = inputToken.nextToken();

System.out.println(nextInput);

//To compare I put like this:

if(nextword.equalsIgnoreCase(nextInput))

System.out.println("Matched");

}

-GK

Kavi_herea at 2007-7-15 5:18:13 > top of Java-index,Other Topics,Algorithms...
# 4
>Let said we have String s = " I went to the beach">and the user input String input = " He went to the mall"wow after the beach and the mallyou go to New To Java Technology then to Algorithms wooooow
katakanaboya at 2007-7-15 5:18:13 > top of Java-index,Other Topics,Algorithms...
# 5

Hi,

If the two strings only contain alphanumerics then there is a very easy approach that uses regular expressions rather than StringTokenizer. The following illustrates -

public class FunnyStringEquality

{

public static void main(String[] args)

{

try

{

String template = "this is a funny string";

String target1 = "how is this funny thing this is";

String target2 = "should not match";

System.out.println(target1 + " " + matches(template, target1));

System.out.println(target2 + " " + matches(template, target2));

}

catch (Exception e)

{

System.out.println("Problem, exception = " + e);

e.printStackTrace(System.out);

}

}

static public boolean matches(String template, String target)

{

String regexSring = ".*[ ]+(" + template.replaceAll("[ ]+","|") + ")[ ]+.*";

return (" " + target + " ").matches(regexSring);

}

}

Roger

sabre150a at 2007-7-15 5:18:13 > top of Java-index,Other Topics,Algorithms...