Click to See Complete Forum and Search --> : While Loop


Xtrme_XJ
11-10-2008, 01:41 AM
I'm sorry if this is a Trivial Question I have not done Java in forever and cannot for the life of me figure out what I am doing wrong... It may Very Well be that it is 1:35am...

I am not getting out of the loop even if I Press 1 or 2 or 3 or 4 or 5

String ans="";
do {
ans = JOptionPane.showInputDialog(null, "Choices are:\n(1) Add new employee\n(2) List employees\n" +
"(3) Display infomration about about an employee\n" +
"(4) Display average wage of employees\n(5) Quit\n\n" +
"What is your choice?\n");

if(!ans.equals("1")||!ans.equals("2")||!ans.equals("3")||!ans.equals("4")||!ans.equals("5"))
JOptionPane.showMessageDialog(null, "");

} while (!ans.equals("1")||!ans.equals("2")||!ans.equals("3")||!ans.equals("4")||!ans.equals("5"));
JOptionPane.showMessageDialog(null, "out");

Thanks for your understanding of my easy question...

scragar
11-10-2008, 03:18 AM
That's a logic error, if the answer is not 1, or it's not 2, or it's not 3... no matter what you put in the loop always evaluates to true, it's impossible for an answer to be all 5 values.

Replace the || with && to use and instead of or and it should start working as you expect.