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...
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'...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"); } } }
Anyone know why this happens and how to fix this?
Use AND, not OR. Think about it
Posted via CodeCall Mobile
Here is a possible solution :X
Enjoy !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'); } }
Cheers !
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks