Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-21-2008, 02:41 AM   PM User | #1
webguy08
Regular Coder

 
Join Date: Mar 2008
Posts: 136
Thanks: 39
Thanked 1 Time in 1 Post
webguy08 is an unknown quantity at this point
Response

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?
webguy08 is offline   Reply With Quote
Old 10-21-2008, 04:55 PM   PM User | #2
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
you mean like
Code:
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?
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow
shyam is offline   Reply With Quote
Old 10-21-2008, 05:58 PM   PM User | #3
webguy08
Regular Coder

 
Join Date: Mar 2008
Posts: 136
Thanks: 39
Thanked 1 Time in 1 Post
webguy08 is an unknown quantity at this point
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"
webguy08 is offline   Reply With Quote
Old 10-22-2008, 01:13 AM   PM User | #4
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
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:

Code:
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
   }
}
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
Aradon is offline   Reply With Quote
Old 10-22-2008, 05:45 PM   PM User | #5
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
or us a nifty regex
Code:
public void chat(String talk) {
  if(talk.matches("Hi|Hello|Aloha")) {
    System.out.println("(NLP): Hello.");
  }
}
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow
shyam is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:50 PM.


Advertisement
Log in to turn off these ads.