• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Input Validation and my struggles with it

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.**
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Snipe Anthony
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it need to be ||?
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Say it out loud...
or
 
Snipe Anthony
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah that makes sense even more when I do say it out loud but the problem is it still doesn't output.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Snipe Anthony
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have not, I will try.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Do you see this output appear anywhere?
 
Snipe Anthony
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it shows up in the system output.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then JOptionPane is not the solution (though you might want it at a later date).
 
Snipe Anthony
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a question, why would it matter if I could see that message in the system output or not? (Future Reference)
 
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Snipe Anthony
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Snipe Anthony
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "invalid input, number has to be positive" output. It still won't register and execute.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Snipe Anthony
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
Snipe Anthony
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant:
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,but you miss one arguments for showMessageDialog method.It requires four arguments
 
Snipe Anthony
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay so I am a little confused, where would I put that?
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

Just add a string as dialog title
 
Snipe Anthony
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I updated the code to:

No errors but it still won't output
 
Snipe Anthony
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoa. sorry computer is spazzing out. Didn't mean to post that 3 times, sorry!
Staff note (Paul Clapham) :

No problem Snipe, I deleted two of them.

 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

Output not showing up on both ?
 
Snipe Anthony
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works in my PC
 
Snipe Anthony
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will you post the entire source code so I can see if I have everything done correctly?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

But the OP was already using <=
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic