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 4 of 4

Thread: If? Or ELSE!

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default If? Or ELSE!

    Howdy,
    Here is my problem, I have to ask the 'user' of the program a yes or no question. They must answer yes or no, therefore a scanner class is sufficient here, done all that. But, now I want my program to tangent in different ways depending on their answer. If they answer no, NO, NO!!!, nO or n, I want all these to work if they answer like this, same for Yes, Y, yes etc.

    If they answer yes I want my program to be able to continue, if they answer no, I want it to end how I please. I just don't know whether or not i need to use "if" or "else", or some sort of loop or what?

    This may be a very simple problem for you guys but I am new to java, if anyone could give me an answer to these problems It would mean alot, thanks.

    Creeeds

  2. #2
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: If? Or ELSE!

    If-else would be easiest . A regex could maybe work very well.
    Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
    Even though there may be a better and simpler way (not that good when it comes to regex or string stuff, lol).

    EDIT:

    Example that may be easier. Going to list the steps since it is from my head and not from any code i have made. All steps should be fairly simple to do.
    1. Convert the string to lower case (basically just use the method "toLowerCase").
    2. Remove whitespaces from the start and end of the string (basically just call the method "trim").
    3.
    a) Check if it starts with n or no.
    b) Check if it starts with y or yes.
    4. Check if you only have ! left.
    5. If all the checks above comes out true then you should be set to go, otherwise ask again.
    Hopefully that would work, but I havenīt tested it, so I donīt know, lol.
    Last edited by Kerr; March 24th, 2011 at 08:15 AM.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: If? Or ELSE!

    lol, I have no idea how to do that.
    Would be able to write a quick code for an example?

    Thanks

    Creeeds

  4. #4
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: If? Or ELSE!

    It is easy. To convert the string to lower case:
    str = str.toLowerCase();
    To remove whitespaces at the beginning and the end.
    str = str.trim();
    Check if a string starts with something:
    if (str.startsWith( "something" ))
        // Code here, lol
    The last one I am not sure about, but you could replace the ! character with nothing like this:
    str = str.replace("!", "");
    And then check the length with the .length() method of the string. Of course, there may be better ways and I do not have the time to actually check if it works now because I am in a hurry. Hope this cleared things up.