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: help with detecting certain characters

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

    Default help with detecting certain characters

    So i have a do while loop and i have user input coming in to detect which character the user entered...i want it to loop until the user enters a valid character...q or Q quits the program, whereas r,R,c,C are all valid characters. If a different character, for example 'L' were entered, it should loop back until a proper character is entered...I'm a beginner and i simply don't know the commands or logic behind this to be able to do this and any help would be greatly appreciated.


    Here is what I have:
     	do {
    			input = JOptionPane.showInputDialog("Please enter shape of pizza, R or C (Q to quit)");
    			if(input == null || input.toUpperCase().equals("Q")) {
    			    return;
    			}
    			} while (input.toUpperCase().equals("C") || !input.toUpperCase().equals("R"));
     
    			shape = input;
    Last edited by goochasauras; September 27th, 2012 at 03:23 PM.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    983
    Thanks
    7
    Thanked 207 Times in 183 Posts

    Default Re: help with detecting certain characters

    The general idea of your code is OK.

    The while condition should not have anything to do with "Q", however. Also a "bad" input like "L" is not "C" AND it is not "R" etc.

    ---

    if(input == null || input.equals("q") || input.toUpperCase().equals("Q")) {

    This is more neatly written as

    if(input == null || input.toUpperCase().equals("Q")) {

    The toUpperCase() takes care of "q" and "Q" wth one check. A similar idea can be used in your while condition.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with detecting certain characters

    I edited to code so it is a little cleaner like you mentioned...but how do i get, in the while part of the statement, the program to loop until c or r are entered but still quitting when q is entered it's a problem only in the while statement i believe

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    983
    Thanks
    7
    Thanked 207 Times in 183 Posts

    Default Re: help with detecting certain characters

    There are two things going on here. First the return when q is entered is taken care of by the if statement. The second is to keep looping while input is not c/r, and that's what the while condition is supposed to ensure.

    Did you understand what I said about AND rather than OR?

    If your code is not doing what you expect, post the updated version and describe what it is doing as you test it.

Similar Threads

  1. Problem with detecting Math operation.
    By BleedObsidian in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 4th, 2012, 11:04 AM
  2. [SOLVED] detecting pc type
    By usama8800 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 26th, 2012, 03:17 PM
  3. Collision Detecting
    By iams3b in forum AWT / Java Swing
    Replies: 0
    Last Post: February 6th, 2011, 01:48 AM
  4. [SOLVED] Detecting whether the piece of code is executing?
    By vivek1982 in forum Web Frameworks
    Replies: 4
    Last Post: March 27th, 2009, 07:17 AM