Results 1 to 6 of 6
  1. #1
    gundershot is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Arrays and if statements

    hey everyone, im very new to java, and i thought it might be interesting to try to make an AI which you can talk casually to, ive made it so that when it asks you a yes or no question you can say things like yeah, sure, okay, nope, nah and so on, but the way ive done this is by making an if statement which looks somewhat like this:

    if(timestable.equals("y")||timestable.equals("Y")| |timestable.equals("yes")||timestable.equals("Yes" )||timestable.equals("Yep")||timestable.equals("ye p")||timestable.equals("yeah")||timestable.equals( "Yeah")||timestable.equals("okay")||timestable.equ als("Okay")||timestable.equals("Sure")||timestable .equals("sure"))

    i was wondering if it was maybe possible to put all of these strings into an array, and make it so that you can say any one of those, without needing to say all of them, and that will make it possible to make the if statement look something like this:

    if(timestable.equals(array))

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Arrays and if statements

    Use a Set that contains all synonyms for "yes", in, say, uppercase; you can do this:

    Java Code:
    if (yesSet.contains(answer.toUpperCase()))
       ...
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Arrays and if statements

    I would map them to a single affirmative result, so all those would be "translated" into a single thing the rest of the code can understand (say "Y" for all the above).
    This Translator can then hold all the unpleasantness, which you can add to or change later on to use a database maybe, or file or whatever.

    So you get the user response and call translate() which gives you a far more limited set of results.
    If it can't find a translation then it can simply return the original text.

    Note, this is just off the top of my head. I usually hide these things away so the main code flow looks neater and I can always rework the messy stuff later as I get better ideas of what to do.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Arrays and if statements

    I write too much...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    gundershot is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: Arrays and if statements

    hey JosAH, thanks for the response, sets sound useful for what i'm doing, but im not sure how to use them D:

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Arrays and if statements

    Quote Originally Posted by gundershot View Post
    hey JosAH, thanks for the response, sets sound useful for what i'm doing, but im not sure how to use them D:
    There are tutorials and there's always the API documentation ...

    kind regards,

    Jos

    ps. @Tolls: tl;dr ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Copying Single Arrays to 2-D Arrays
    By jmscarlet9 in forum New To Java
    Replies: 7
    Last Post: 04-03-2012, 12:17 AM
  2. If statements and arrays, problem
    By Madlollipop in forum New To Java
    Replies: 3
    Last Post: 11-12-2011, 01:30 PM
  3. Arrays and For statements for Images in Applet
    By SummitWhiteMaro in forum Java Applets
    Replies: 0
    Last Post: 03-07-2011, 04:59 AM
  4. Help with if else statements
    By np2392 in forum New To Java
    Replies: 2
    Last Post: 09-24-2010, 02:25 AM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM

Tags for this Thread

Posting Permissions

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