Go Back   CodeCall Programming Forum > Software Development > Java Help
Register Blogs Search Today's Posts Mark Forums Read

Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-04-2009, 08:15 PM
Newbie
 
Join Date: Jul 2009
Location: Honolulu, Hawaii
Posts: 2
kyungjin is an unknown quantity at this point
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-04-2009, 08:20 PM
marwex89's Avatar
Code Warrior
 
Join Date: Jul 2008
Location: Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
Posts: 9,849
marwex89 is a glorious beacon of lightmarwex89 is a glorious beacon of lightmarwex89 is a glorious beacon of lightmarwex89 is a glorious beacon of lightmarwex89 is a glorious beacon of lightmarwex89 is a glorious beacon of light
Send a message via AIM to marwex89 Send a message via MSN to marwex89
Re: Simple Program - Use of "OR" in While Loops

Use AND, not OR. Think about it

Posted via CodeCall Mobile
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-05-2009, 01:28 AM
Turk4n's Avatar
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 20
Posts: 3,822
Turk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud of
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
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 ~❤❤❤
初音ミク。~❤❤❤
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-05-2009, 02:01 AM
Newbie
 
Join Date: Jul 2009
Location: Honolulu, Hawaii
Posts: 2
kyungjin is an unknown quantity at this point
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-05-2009, 02:15 AM
Turk4n's Avatar
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 20
Posts: 3,822
Turk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud of
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
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 ~❤❤❤
初音ミク。~❤❤❤
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
[HELP] C++ program that will ask the user to enter not greater than to 15 numbers Kyram143 C and C++ 8 12-16-2009 02:53 PM
Tutorial: Starting C# with C# 2008 Express Edition Jordan CSharp Tutorials 20 07-27-2009 05:45 AM
Debugging a C++/C File with GDB Debugger awesome001 C and C++ 2 01-01-2009 08:07 PM
Creating simple Notition store program Coldhearth Java Help 1 12-10-2008 04:44 PM
Fairly Simple Program Request Fodder MarketPlace 4 12-22-2007 12:20 AM


All times are GMT -5. The time now is 11:40 PM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0