Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Response

  1. #1
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post

    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?

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    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

  • #3
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post
    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"

  • #4
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    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

  • #5
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •