jQuery in Action, 3rd edition
The moose likes Swing / AWT / SWT and the fly likes Help with creating GUI Error Messages. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help with creating GUI Error Messages." Watch "Help with creating GUI Error Messages." New topic

Help with creating GUI Error Messages.

Ryan Childs

Joined: Jan 27, 2012
Posts: 2
I have an assignment where I must modify my code to:
A: If the weight of an object is greater than 99999 your code should print a GUI driven error message, and force the user to reenter the weight of the object.
B: If a user enters a negative weight, your code should display a GUI warning message.

My original code is:

Not sure if its needed, but the Weight class is:

I thought in order to add the error message pop ups it would require an IF statement. I came up with:

The entire code with the addition looks like:

When I run the program it runs just the original program. It does not recognize if I enter a number higher than 99,999 or less than 0 and performs the calculations as normal. I am completely unsure as to what I am missing. Any help pointing me in the right direction would be greatly appreciated.
Greg Brannon

Joined: Oct 24, 2010
Posts: 563
Did you not understand my suggestion in the other forum? If not, please describe your confusion and ask additional questions in the original post.


My suggestion from the other forum:

You need some kind of loop to repeat the request for the object's weight if the user's entry is incorrect, something like:

Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Ryan Childs

Joined: Jan 27, 2012
Posts: 2
Haha, no, just thanks to the Wifey I lost browser history and could for the life of me not recall the site.

Anyways, you suggested the do-while loop.

I was working on that and could not figure out the proper way to construct it.

I understand I need to have for lets say the "A" portion:

However the bit about setting parts to false and true completely lost me. Also I am unsure where the do-while loop is suppose to fit in the original code.

Again my bad on switching to a different site, awesome that you are here as well though! (would love the name of that other site too)
Greg Brannon

Joined: Oct 24, 2010
Posts: 563
You have the construct of the do/while loop correct, and you could do it the way you've coded. I suggested using an error flag, a boolean called 'error', so that multiple conditions could be tested in a single loop with the minimum or simplest (in my mind) code. Since you want to test for both greater than and less than a certain amount, your while statement should look like:

while( ObjectWeight > 99999 || ObjectWeight < 0 );

so that the do loop will continue to execute, asking the user for the object's weight, until a correct number is entered.

I'll PM you the other site.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
perhaps you could work (compare notes) with this poster

I agree. Here's the link: http://aspose.com/file-tools
subject: Help with creating GUI Error Messages.
It's not a secret anymore!