+ Reply to Thread
Results 1 to 5 of 5

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

  1. #1
    Newbie kyungjin is an unknown quantity at this point
    Join Date
    Jul 2009
    Location
    Honolulu, Hawaii
    Posts
    2

    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. #2
    Code Warrior marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89's Avatar
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,158
    Blog Entries
    2

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

    Use AND, not OR. Think about it

    Posted via CodeCall Mobile

  3. #3
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,836
    Blog Entries
    4

    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 !

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  4. #4
    Newbie kyungjin is an unknown quantity at this point
    Join Date
    Jul 2009
    Location
    Honolulu, Hawaii
    Posts
    2

    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

  5. #5
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,836
    Blog Entries
    4

    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 !

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Replies: 8
    Last Post: 12-16-2009, 01:53 PM
  2. Tutorial: Starting C# with C# 2008 Express Edition
    By Jordan in forum CSharp Tutorials
    Replies: 20
    Last Post: 07-27-2009, 04:45 AM
  3. Debugging a C++/C File with GDB Debugger
    By awesome001 in forum C and C++
    Replies: 2
    Last Post: 01-01-2009, 07:07 PM
  4. Creating simple Notition store program
    By Coldhearth in forum Java Help
    Replies: 1
    Last Post: 12-10-2008, 03:44 PM
  5. Fairly Simple Program Request
    By Fodder in forum MarketPlace
    Replies: 4
    Last Post: 12-21-2007, 11:20 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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