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 ?
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
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
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
>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
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