[Logo]
Forums Register Login
Input Validation and my struggles with it
Okay so I posted on here previously and people helped me out tremendously. I only have only one issue with my code now that I can't seem to figure out. That is how to add an input validation "invalid input, number has to be positive" dialog, after a user input of 0 or a negative number for length (tLength) and width (tWidth) to find the area.

Here is my code which runs fine, just needs the validation added:



I've tried:



And it received errors for  "Bad operand for binary operator '<='". I believe because tLength and tWidth are both String.

So then I tried:



It accepted it but didn't change the output when I put a 0 or negative number into the Length or Width field.

So finally I tried:


Also, no errors but it didn't work when negative numbers or 0 were entered.

**If someone can enter me in the right direction it would be much appreciated, I feel I have been stuck for days.**
Your problem is "&&".
Does it need to be ||?
(1 like)
Say it out loud...
or
Yeah that makes sense even more when I do say it out loud but the problem is it still doesn't output.
I don't see a problem with your last code snippet other than &&. You might want to throw in some prints for getText() to see if you get what you expect. I would also suggest initializing area to zero before entering the try block.
 

Snipe Anthony wrote:the problem is it still doesn't output.

Your problem may be that you're mixing a GUI interface with a text interface. Have you tried using a JOptionPane to display the error message?
I have not, I will try.

Do you see this output appear anywhere?
Yes it shows up in the system output.
Then JOptionPane is not the solution (though you might want it at a later date).
Just a question, why would it matter if I could see that message in the system output or not? (Future Reference)
 

It accepted it but didn't change the output when I put a 0 or negative number into the Length or Width field.


Beside the || operator, you also need to change the operator from < to <= to detect value 0
I changed that as well and it still won't output. Is this the john joe from javaprogrammingforums? If so it's Snipe from yesterday hah.
 

Snipe Anthony wrote:I changed that as well and it still won't output. Is this the john joe from javaprogrammingforums? If so it's Snipe from yesterday hah.

Yes, is me
What output is not showing up ?
The "invalid input, number has to be positive" output. It still won't register and execute.
 

Snipe Anthony wrote:The "invalid input, number has to be positive" output. It still won't register and execute.


In this line
The output will shows in system only. If you want to display it as dialog, use JOptionPane instead
 

John Joe wrote:

Snipe Anthony wrote:The "invalid input, number has to be positive" output. It still won't register and execute.


In this line
The output will shows in system only. If you want to display it as dialog, use JOptionPane instead



So would it be like:
I meant:
Yes,but you miss one arguments for showMessageDialog method.It requires four arguments
Okay so I am a little confused, where would I put that?
 

Snipe Anthony wrote:Okay so I am a little confused, where would I put that?

Just add a string as dialog title
I updated the code to:

No errors but it still won't output
Whoa. sorry computer is spazzing out. Didn't mean to post that 3 times, sorry!
 

Snipe Anthony wrote:Whoa. sorry computer is spazzing out. Didn't mean to post that 3 times, sorry!

Output not showing up on both ?
Neither one of the outputs come up in a dialog box when I enter a negative number or 0. So frustrating, I am about to call it quits.
It works in my PC
Will you post the entire source code so I can see if I have everything done correctly?
 

John Joe wrote:. . . you also need to change the operator from < to <= to detect value 0

But the OP was already using <=
Only use null as the first argument for a show dialogue method if you don't have a GUI; null will make the dialogue appear in the centre of the screen. Use the name of a GUI component you want the dialogue to appear in the same location as (middle of the component named). Does the display become unresponsive and no dialogue appear? In that case minimise everything on your screen; I have known such dialogues to appear behind something and be invisible.
 

Campbell Ritchie wrote:But the OP was already using <=

The OP had remove that <= operator when he used try and catch in the latest code
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards


This thread has been viewed 264 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jan 16, 2018 02:30:28.