Closed Thread
Results 1 to 5 of 5

Thread: Simple Program - Use of "OR" in While Loops

  1. #1
    kyungjin is offline Newbie
    Join Date
    Jul 2009
    Location
    Honolulu, Hawaii
    Posts
    2
    Rep Power
    0

    Simple Program - Use of "OR" in While Loops

    Okay, so I have a relatively simple program. All it does is check to see if the letters 'Q' or 'q' is typed into an input. If it is, the program terminates... if not, the program keeps continuing until 'Q' or 'q' is entered in.

    I tried using an "if" statement coupled with a "break" and that seems to work inside of the "while" loop... except, I want this program to work with using the "break"... if you know what I mean... Here's the sample code below...
    Code:
    import java.util.Scanner;
    
    class testclass
    {
    	public static void main(String args[])
    	{
    		char choice;
    		Scanner myScanner = new Scanner(System.in);
    
    		System.out.print("Type Q to Quit: ");
    		choice = myScanner.findInLine(".").charAt(0);
    		System.out.print("\n");
    		
    		while((choice != 'Q') || (choice != 'q'))
    		{
    			System.out.print("Type Q to Quit: ");
    			choice = myScanner.findInLine(".").charAt(0);
    			System.out.print("\n");
    		}
    	}
    }
    As of right now... The program compiles and builds correctly... but everytime I execute in the Eclipse Console, anything I enter doesn't seem to terminate the program, even if I type in 'Q' or 'q'. This is weird because whenever I put in "while(choice != 'Q')" or "while(choice != 'q')" it seems to work (when I type the respective keys)... but not when I put it together and connect it with a conditional 'OR'...

    Anyone know why this happens and how to fix this?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: Simple Program - Use of "OR" in While Loops

    Use AND, not OR. Think about it

    Posted via CodeCall Mobile

  4. #3
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,849
    Blog Entries
    4
    Rep Power
    49

    Re: Simple Program - Use of "OR" in While Loops

    Here is a possible solution :X

    Code:
    import java.util.Scanner;
    class TestCLass	{
    	public static void main(String args[])	{
    		char choice;
    		String in;
    		Scanner myScanner = new Scanner(System.in);
    		
    	do {
    		System.out.print("Type Q to Quit: ");
    		in = myScanner.next();
    		choice = in.charAt(0);
    	if(choice == 'Q' || choice == 'q') {
    		break;
    	}
    	else 
    		continue;
    	}
    		while(choice != 'Q' || choice != 'q');
    			
    	}
    }
    Enjoy !
    Cheers !

  5. #4
    kyungjin is offline Newbie
    Join Date
    Jul 2009
    Location
    Honolulu, Hawaii
    Posts
    2
    Rep Power
    0

    Re: Simple Program - Use of "OR" in While Loops

    Oh man...... I can't believe something like that slipped my mind =_=;;;;

    Makes me feel uber stupid right now... Thanks a lot. That solved my problem *sigh* hahaha

  6. #5
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,849
    Blog Entries
    4
    Rep Power
    49

    Re: Simple Program - Use of "OR" in While Loops

    Quote Originally Posted by kyungjin View Post
    Oh man...... I can't believe something like that slipped my mind =_=;;;;

    Makes me feel uber stupid right now... Thanks a lot. That solved my problem *sigh* hahaha
    No problem, kindly rep+ me :>
    And your not stupid, brain farts happens to anyone !

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 06-22-2011, 09:57 PM
  2. Replies: 8
    Last Post: 12-25-2010, 04:17 PM
  3. Replies: 2
    Last Post: 11-01-2010, 11:52 PM
  4. Replies: 3
    Last Post: 07-26-2010, 04:49 PM
  5. Replies: 0
    Last Post: 03-18-2010, 04:42 PM

Tags for this Thread

Bookmarks

Posting Permissions

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