Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: how to write answerable questions into java??

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to write answerable questions into java??

    sorry if the title wasnt clear.
    i want to be able to write a question into the program which will respond differently depending on what is entered by the user.
    ie. Do you want to continue?
    then the user can then either enter yes or no.
    then depending on what they enter diffent code starts.

    what i have tried is this.

    screen.println( "Would you like to continue?" );
    answer = keyboard.readLine()

    if (answer == yes)
    {
    Further code is entered here
    }

    if (answer == no)
    {
    screen.println("okay");
    }

    this was my general idea i am not sure if this can be fixed or is it completely wrong.

    Any help is apperciated, and thank you in advance


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: how to write answerable questions into java??

    Never use == to compare string values. readLine returns a string I'm assuming. [Not sure what class 'keyboard' is, scanner uses nextLine]

    Compare the answer to some predefined value using string comparison, EG
    if(answer.toLowerCase().equals("yes"))
    {
     ...
    }

    Please post the entire code, or an SSCCE [Short Self-Contained Complete Example] that demonstrates what you are attempting to do, however I will first point you to the
    java tutorials.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: how to write answerable questions into java??

    What is the screen object in your code:
    screen.println("....");

    Your general idea can work, except you won't want to use == to compare Strings since == tests to see if two String *objects* (or any objects for that matter) are one and the same. Rather you want to check if two Strings contain the same content -- the same letters and in the same order, and for that you should use the equals(...) or equalsIgnoreCase(...) methods. Also your String literals should be surrounded by "", and so you'll be checking to see if one Strings holds "yes" or "no":

    // we'll use equalsIgnoreCase since we don't care
    // if the user types in "yes" or "Yes" or "yeS"
    if ("yes".equalsIgnoreCase(answer)) {
      // do something
    }

    Note that I prefer the above code to this:

    if (answer.equalsIgnoreCase("yes")) {
      // do something
    }

    because with the former code, I reduce the chances of getting a NullPointerException in case answer is null.
    Last edited by Fubarable; July 4th, 2012 at 11:06 AM.

Similar Threads

  1. Best IDE for java programming?(and more questions)
    By Hamed in forum Java Theory & Questions
    Replies: 6
    Last Post: February 13th, 2012, 12:28 AM
  2. Help in java questions
    By Normal9ja in forum Java Theory & Questions
    Replies: 3
    Last Post: September 21st, 2011, 08:42 PM
  3. Noobie to Java questions!! :D
    By chansey123 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 17th, 2010, 11:53 AM
  4. Java Questions! :)
    By xs4rdx in forum Java Theory & Questions
    Replies: 0
    Last Post: February 21st, 2010, 08:40 AM
  5. Few very basic Java questions.
    By 01001010 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 13th, 2010, 01:14 PM