Basically what you have here is a PART of my program. Here I am supposed to get the LENGTH of a rectangle from a user and return that length to the main method for further use.
My program works fine if I take out the input validation but I must have input validation on the length that does NOT allow null entries, A-Z or a-z, or special chars and does NOT allow input of less than 1. Basically only numerical values are allowed.

However, if you were to run this loop I made it does not work right even though to me it seems that it should be fine. It is stuck in the loop for some reason. I have not got the less than one in there right now either.

SO, any suggestions would help and I MUST use while/for/if logic on this code. I know that using try and catch works but I am supposed to use what the instructor has given us.

   		The getUserLength method requests the user to enter a number to use as 
   		the rectangle length.
   		@return The number for the length entered.
       public static double getUserLength()
         final String USER_LENGTH = "Please enter the length of the rectangle."; // holds string to request input
         String input = ""; 	// to hold user input
         double length = 0;         // to hold number vaule of length
         boolean goodNum;
         int counter = 0;
         int i= 0;
         while (input.equals(""))
            input = JOptionPane.showInputDialog(null, USER_LENGTH, null, JOptionPane.QUESTION_MESSAGE); // request length
         } // checks for null entry
         goodNum = true; 		// set flag for correct data input
         counter = 0; 			// keeps position within string
         while (goodNum && counter < input.length())
            for (i = 0; i < input.length(); i++)
               if ((input.charAt(i) >='A') || (input.charAt(i) <='Z') && (input.charAt(i) >='a')
               || (input.charAt(i) <='z'));    
                  goodNum = false; // set flag to incorrect data type
               }	// End of IF STATEMENT
               counter++; // increment to next position in string
            }		// End of FOR STATEMENT
            while (!goodNum)
               input = JOptionPane.showInputDialog(null,USER_LENGTH,null,JOptionPane.ERROR_MESSAGE); // gives error
               goodNum = true;  // reset flag to true
               counter = 0;         // set counter back to zero
            } // end while !goodNum
         } // end while goodNum and counter < length
         length  = Double.parseDouble(input); // parse input into a double named length
         return length; // return length to main method
      } // End of getUserLength method

Why don't you try this simple thing:

String input = "";
double number = 0.0;
boolean goodNumber = false;

while (!goodNumber)
  input = JOptionPane.showInputDialog(null, USER_LENGTH, null, JOptionPane.QUESTION_MESSAGE); // request length

  try {
     input = input.trim();
     number = Double.parseDouble(input);
     goodNumber = true;
  } catch (NumberFormatException nfe) {
     goodNumber = false;
  } catch (Exception e) {
     goodNumber = false;
     // I don't remember the API of the JOptionPane. This is in case the input.trim() gives you a NullPointerException
This article has been dead for over six months. Start a new discussion instead.