Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Am learning java and was assigned to create a simple chat bot meant to give quick replies but when i pick say "1" meant for the third or fourth preset questions, i still get the first set of preset questions.i would also like to create answers where the user will be answered after picking a number.Kindly assist to make it work such that when i pick say "1" am able to pick the next "1" of this 1 and so on.Please.also i cant pick number 10 and 11.


What I have tried:

<pre>public class deskChat
  {
  /**
     * Get a default greeting
   * @return a greeting
   */
  public String getGreeting()
  {
    return "Hello XX.\nThese chats will be saved as per our privacy policy.Click here to view https://www.reditttt.com\nRespond with a number to select any options below.\n1.Unable to connect.\n2.Doctor Unavailable.\n3.Disconnected.\n4.Insurance.\n5.Medication.\n6.Appointments.\n7.Mpesa / Debit card.\n8.How to see a doctor.\n9.Change details.\n10.Referrals.\n11.Any other";
  }
  /**}
   * Givrees a response to a user statement
   *
   * @param statement
   *            the user statement
   * @return a response based on the rules given
   */
  public String getResponse(String statement)
  {
    String response = "";//.;,.................................................           vb 6 mm
    if (statement.indexOf("1") >= 0)
    {
      response = "Select any of the options below,\n1.Server not responding.\n2.Facial recognition failure.\n3.Insurance not working.\n4.Appointment not going through.\n5.Doctor unavailable or assisting others.\n6.Unable to make payment.\n7.Any other.\n8.Main menu.";
      
      
    }
    else if (statement.indexOf("2") >= 0)
        
    {
      response = "Select any of the options below.\n1.Doctor offline.\n2.Doctor assisting others\n3.Doctor not on the pannel\n4.Any other.\n5.Main menu";
    }
    else if(statement.indexOf("3")>= 0)
    {
      response ="Select any option below,\n1.Network issues.\n2.Phone went off.\n3.Any other\n4.Main menu";
    }
    else if (statement.indexOf("4") >= 0)
    {
     response = "Select any option below,\n1.SASAdoctor Programmes.\n2.Verify Insurance.\n3.Policy record not found.\n4.Server not responding.\n5.Unable to use insurance.\n6.Billing issues.\n7.Any other.\n8.Main menu";
    }
    else if(statement.indexOf("5") >=0)
    {
      response = "Select any option below,\n1.Get prescription from us.\n2.Upload your prescription.\n3.Medication delay.\n4.Medication delivery Turn Around Time.\n5.Location not found.\n6.Any other.\n7.Main Menu.";
    }
    else if (statement.indexOf("6") >= 0)
    {
      response = "Select any of the options below,\n1.Book appointment.\n2.Already booked.\n3.Reschedule appointment.\n4.Unable to use pending appointment.\n5.Any other.\n6.Main menu";
    }
    else if(statement.indexOf("7")>= 0)
    {
       response = "Select any of options below,\n1.How to pay via Mpesa.\n2.Mpesa / Debit card not working.\n3.Paid but not connected.\n4.Multiple deduction.\n5.Any other.\n6.Main menu";
    }
    else if (statement.indexOf("8")>= 0)

    {
      response = "Select any of the options below,\n1.Book appointment.\n2.Instant consult.\n3.Main menu";
    }
    else if (statement.indexOf("9")>= 0)
    {
      response = "Select any of the options below,\n1.Change photo.\n2.Password change.\n3.Change names .\n4.Change Id.\n5.Change insurance\n6.Any other.\n7.Main menu";
    }
    else if (statement.indexOf("10")>= 0)
    {
      response ="Select one of the options below,";
    }
    else
    {
      response = "Go back to main menu";
    }
    return response;
  }

  }/**
   * Pick a default response to use if nothing else fits.
   * @return a non-committal string
   */
Posted
Updated 26-Jul-21 23:37pm
v2
Comments
Ridoh Set 27-Jul-21 5:25am    
Thanks,,my bad.The "while" was not supposed to be there but instead "if".
I appreciate your response and will try to adopt that.
Richard MacCutchan 27-Jul-21 5:48am    
As already suggested that is completely the wrong way to do it. You should accept an integer value from the user, and use a switch statement to execute the correct set of instructions.

That code won't even compile - which is probably why its doesn't work as expected!
Unless your code compiles with no errors, it doesn't produce an executable file - so when you try to run the "latest code" what executes is the previous version that did.

Java does not have any concept of while ... else ... so that code doesn't compile.

In addition, if you look for "1" in a string and find it, you will never get to check for "10" in the same string. Conversely, if you look for "1" in a string and don't find it, you will never find a match for "10" in the same string!

What I would do is stop faffing with strings: when the user enters a menu option, convert it to an integer (reporting a problem if what he entered is not convertible) and pass that numeric value to your function instead.
I'd also suggest that you look at the switch[^] statement as well, instead of a series of "else if" lines. Or use an array of strings instead of that code ...
 
Share this answer
 
Comments
Ridoh Set 27-Jul-21 5:32am    
Would you kindly show me how to fix it?Thanks.
OriginalGriff 27-Jul-21 6:00am    
Throw it in the bin and start again? But thinking and planning first instead of diving into code and getting confused?
To be honest there isn't anything there I'd want to keep...
Ridoh Set 27-Jul-21 7:39am    
Ok Thanks!!
I have no idea where you found that code, but wherever it is I suggest you never go there again. If you want to learn Java properly then go to the Java Tutorials Learning Paths[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900