PDA

View Full Version : Response


webguy08
10-21-2008, 02:41 AM
Hi all,
I am writing Java code where I would like the desired responses applied to the correct statments made by the user.

I have the following code, which works correctly:

public void chat(String talk)
{
String intro = "Hi"; **Local Variable**
if(talk.equals(intro)) {
System.out.println("(NLP): Hello.");
}

However, on the String intro = "Hi"; line I would like to include other words, as well as "Hi". However I do not know how. I have experimented with it; using || , + () etc. and haven't found a solution which works.

Can anyone help?

shyam
10-21-2008, 04:55 PM
you mean like
public void chat(String talk, String user) {
String intro = "Hi" + " " + user;
if(talk.equals(intro)) {
System.out.println("(NLP): Hello.");
}
}
or did you mean something else entirely?

webguy08
10-21-2008, 05:58 PM
I mean like the term intro would have multiple words associated with it. So if a user were to enter one of those multiple words the response would be "Hello" :)

Aradon
10-22-2008, 01:13 AM
You could either use regular expressions in Java using the pattern and matcher classes, or you could just create other variables and test for them.

Another way would to be to create an array that would hold each of these strings, then compare through each of these strings with a for loop and check if it's in there.

Or if you wanted it to be more dynamic and extensible, you could create an ArrayList!

Anyways, psuedocode for the array:


public class test
{
main(String)
{
declare array w/ all strings in it
get input
loop through array till you find it
if found
print hello
break
end loop
}
}

shyam
10-22-2008, 05:45 PM
or us a nifty regex

public void chat(String talk) {
if(talk.matches("Hi|Hello|Aloha")) {
System.out.println("(NLP): Hello.");
}
}