Reputation: 1
i am having trouble validating this part of my code ,the error message is not diplaying correctly and if i only hit the enter key the program will exit, any help is appreciated.
strInput1="";
strInput1 = JOptionPane.showInputDialog(null,
"2013 SHIPPING OPTIONS\n\n(O)vernight shipping-$10.00"+
"\n(T)wo-Day shipping-$7.50\n(P)riority shipping-$5.00"+
"\n(N)o cost shipping"+
"\n\nPlease select a shipping option(O,P,T or N) ",
"Wiliam's Party Store",3);
if(!strInput1.equals(""))
JOptionPane.showMessageDialog(null,
"You MUST enter O,o,T,t,P,p,N,n",
"ERROR!!!",0);
cShipping=(strInput1.toUpperCase().charAt(0));
while((!strInput1.equals(""))&& !(cShipping=='P')|(cShipping=='T')||(cShipping=='O'))
{
JOptionPane.showMessageDialog(null,
"You MUST enter O,o,T,t,P,p,N,n",
"ERROR!!!",0);
strInput1 = JOptionPane.showInputDialog(null,
"2013 SHIPPING OPTIONS\n\n(O)vernight shipping-$10.00"+
"\n(T)wo-Day shipping-$7.50\n(P)riority shipping-$5.00"+
"\n(N)o cost shipping"+
"\n\nPlease select a shipping option(O,P,T or N) ",
"Wiliam's Party Store",3);
if (!strInput1.equals(""))
cShipping=(strInput1.toUpperCase().charAt(0));
strInput1 = "N";
}
PO1.setShipping(cShipping);
Upvotes: 0
Views: 94
Reputation: 159844
For multiple negative expressions use the logical &&
operator:
while (!strInput1.equals("") && cShipping != 'P' &&
cShipping != 'T' && cShipping != 'O')
The ||
operator short circuits expressions so the while
loop can remain active even if strInput1
is empty. Also cShipping
is never assigned in the second while
loop which will prevent the loop from exiting.
Aside: A do-while
loop could allow both loops to be merged into one.
Upvotes: 1
Reputation: 1033
So your trouble is validating your code, and not the code itself? I know our first instinct is too rush in and correct your code however I would like to offer an alternative solution I think is more beneficial.
I think the solution to your troubles would be to refactor your code, first learning good coding practice and styles. This would help you in the future as well with any development work.
A good place to start would be here (wikipedia) where they discuss coding conventions and refactoring.
In the code you pasted I see spelling mistakes, a line saying ' enter code here`' and flaws in your logic. Among other things, it is also not clear where your last 'if' statement includes the second line: and while the indent shows that it may, the lack of braces ensures otherwise.
if (!strInput1.equals(""))
cShipping=(strInput1.toUpperCase().charAt(0));
strInput1 = "N";
should be the following.. (if this is what you indeed intended)
if (!strInput1.equals(""))
cShipping=(strInput1.toUpperCase().charAt(0));
strInput1 = "N";
On a side note it would be worth improving on your code by using modularisation, coupling and perhaps even more error checking/catching.
Upvotes: 0
Reputation: 19788
You have a single |
in your code which is a Bitewise Or and not a logical or ||
Upvotes: 0