GPWiki.org
GPWiki.org
It is currently Tue May 21, 2013 6:02 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Input Loop Validation
PostPosted: Wed Feb 23, 2011 4:31 am 
Octogenarian
User avatar

Joined: Wed Aug 20, 2008 1:16 am
Posts: 93
Location: A Large Zoo In Seattle
So I'm learning java...it's pretty cool so far, very similar to the c++.
I've written a short input loop. The user is asked for a value and the loop will give an error if the input is out of range or not a number then ask for the value again.
If the user hits cancel, the loop is finished.

How does this look? Is it bullet proof? Does it follow best practices? How are the variable names?
Thanks in advance for any suggestions.

Code:
      int boardSize = -1;
      
      while(boardSize == -1) {
         try {
            String intStr = (JOptionPane.showInputDialog(null,
                  "Enter an integer of value 1 through " + MAXIMUM_NUMBER_OF_ROWS,
                  "How many rows?",
                  JOptionPane.QUESTION_MESSAGE));
            //check if the user has canceled the input box
            if(intStr == null) {
               break;
            }
            boardSize = Integer.parseInt(intStr);
           } catch (NumberFormatException e) {
             JOptionPane.showMessageDialog(null,
                    "Error: you must enter an integer of value 1 through " + MAXIMUM_NUMBER_OF_ROWS,
                    "Invalid Input",
                    JOptionPane.ERROR_MESSAGE);
             boardSize = -1;
             continue;
           }
          
           if((boardSize > MAXIMUM_NUMBER_OF_ROWS) || (boardSize < 1)) {
             JOptionPane.showMessageDialog(null,
                   "Error: you must enter an integer of value 1 through " + MAXIMUM_NUMBER_OF_ROWS,
                    "Invalid Input",
                    JOptionPane.ERROR_MESSAGE);
             boardSize = -1;
           }
      }

_________________
Mairzy doats and dozy doats and liddle lamzy divey
A kiddley divey too, wouldn't you?


Top
 Profile  
 
PostPosted: Wed Feb 23, 2011 4:57 pm 
Dexterous Droid
User avatar

Joined: Wed Aug 18, 2004 7:40 pm
Posts: 3735
Location: South Africa
Looks fine to me :)

The standard Java naming convention is the camel case just like you've done.

_________________
Whatever the mind can conceive and believe, it can achieve


Top
 Profile  
 
PostPosted: Fri Feb 25, 2011 3:30 am 
Octogenarian
User avatar

Joined: Wed Aug 20, 2008 1:16 am
Posts: 93
Location: A Large Zoo In Seattle
Thanks for the feedback, IGTHORN.
I'm trying to start learning good habits early.

I did run into some trouble with simply breaking out of the loop when the user canceled because it left numberOfDisks equal to -1, which was a really nonsense value.
I was able to fix it by simply returning null (instead of breaking).

I'll put up the full program later as it is really kind of a neat "flying through a tunnel of colors" type effect.

_________________
Mairzy doats and dozy doats and liddle lamzy divey
A kiddley divey too, wouldn't you?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group