evaluting 3 statements in while loop condition


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: evaluting 3 statements in while loop condition

  1. #1
    Join Date
    Nov 2003
    Posts
    29

    evaluting 3 statements in while loop condition

    this is probly the easiest thing u've been asked today if u know java . .

    I have this statement
    Code:
    while ((userInput!='1')&&(userInput!='2')&&(userInput!='3')){
    
    System.out.println("That was not a valid entry");
    	 	userInput=in.readCharacter();
    		}
    the idea is if the user doesnt enter 1,2 or 3 the program will loop til they do. This works okay but each time the program prints the error 3 times. How can aviod this please ?

  2. #2
    Join Date
    Sep 2002
    Posts
    124
    Try using the OR operand instead of the AND... it seems that the WHILE condition is being satisfied THREE times in your predetermined condition.

  3. #3
    Join Date
    Nov 2003
    Posts
    29
    Thats right, i think for each '!=' it prints the line, i tried with 2 conditions and it prints only 2 lines.

    With OR it does not work at all, i think i will have to change the structure of the statement but i dont know how . .

  4. #4
    Join Date
    Nov 2003
    Posts
    22
    You can try this:

    boolean x = false;
    while ( x== false ) {
    userinput=in.readCharacter();
    if ( userinput == 0 || userinput > 3 ) {
    System.out.println("That was not a valid entry");
    }
    else {boolean=true;}
    }

  5. #5
    Join Date
    Nov 2003
    Posts
    29
    yes, ill have to use some sort of boolean test, thanks for idea.

  6. #6
    Join Date
    Feb 2004
    Posts
    808

    Re: evaluting 3 statements in while loop condition

    Originally posted by rix

    I have this statement
    Code:
    while ((userInput!='1')&&(userInput!='2')&&(userInput!='3')){
    if the user doesnt enter 1,2 or 3 the program will loop til they do. [/B]
    the solution is inherent in the statement you just made.. you say "1 or 2 or 3", and then you are using the AND operator in java.. you want an OR

    this code works:
    Code:
    char userInput = '0';
    while ((userInput!='1')||(userInput!='2')||(userInput!='3')){
      System.out.print("Please enter 1, 2 or 3:");
      userInput=in.readCharacter();
    }
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  7. #7
    Join Date
    Nov 2003
    Posts
    29
    thanks but the or doesn't work. in the end i just rewrote the code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center