title: Adding validation to a program - JAVA
body: <p>I have written  the code for a program that asks the user to select a shape, set a number for its radio or width and prints the area of the shape as a result. I am trying to add some validation to it where, for example, the user have to set a number between 10-30 for the radius of the circle and the application continues prompting the user until he/she enters a valid number. I haven't been able to do it so far. Any help?  This is what I have so far: </p>
  
  <pre><code>package shapes;
  
  import javax.swing.JOptionPane;
  
  public class Shapes {
  
  public static void main(String[] arg) {
      mainWindow();
  }
  
  private static void mainWindow() {
      int more = 0;
      String[] buttons = {"Circle", "Rectangle", "Triangle"};
      while (more == 0) {
  
          int userChoice = JOptionPane.showOptionDialog(null,
                  "Select a Shape", "mainWindow Task 1", 
                  1, 3, null, buttons, buttons[2]);
  
          if (userChoice == 0) {
              userCircle(); 
          } else if (userChoice == 1) {
              userRectangle(); 
          } else if (userChoice == 2) {
              userTriangle(); 
          } else {
              System.exit(-1);
          }
  
          more = JOptionPane.showConfirmDialog(null,
              "Any other Shape?", "mainWindow Task 1", 2, 3);
      }
  }
  
  private static void userCircle() {
      String enteredRadius = JOptionPane.showInputDialog(null,
              "Enter a number greater than 0 for the radius of the circle. ", "1");
      double radius = Double.parseDouble(enteredRadius);
      Circle userCircle = new Circle(radius);
      double area = userCircle.getArea();
      double perimeter = userCircle.getCircumference();
      JOptionPane.showMessageDialog(null,
              "A circle with the radius of " + unitTest(radius) + " has\n"
              + "an area of " + unitTest(area) + ",\n"
              + "and a perimeter = " + unitTest(perimeter) + ".");
  }
  
  
  private static void userRectangle() {
      String enteredLength = JOptionPane.showInputDialog(null,
              "Enter a number greater than 0 for the length of the rectangle. ", "1");
      String enteredWidth = JOptionPane.showInputDialog(null,
              "Enter a number greater than 0 for the width of the rectangle. ", "1");
      double length = Double.parseDouble(enteredLength);
      double width = Double.parseDouble(enteredWidth);
      Rectangle userRectangle = new Rectangle(length, width);
      double area = userRectangle.getArea();
      double perimeter = userRectangle.getPerimeter();
      JOptionPane.showMessageDialog(null,
              "A rectangle with a length of " + unitTest(length) + ", and a width of " + unitTest(width) + ",\n"
              + "has an area of " + unitTest(area) + ",\n"
              + "and a perimeter " + unitTest(perimeter) + ".");
  }
  
  private static void userTriangle() {
      String userSideA = JOptionPane.showInputDialog(null,
              "Enter a number greater than 0 for the length for side a. ", 3);
      String userSideB = JOptionPane.showInputDialog(null,
              "Enter a number greater than 0 for the length for side b. ", 4);
      String userSideC = JOptionPane.showInputDialog(null,
              "Enter a number greater than 0 for the length for side c. ", 5);
      double sideA = Double.parseDouble(userSideA);
      double sideB = Double.parseDouble(userSideB);
      double sideC = Double.parseDouble(userSideC);
      Triangle userTriangle = new Triangle(sideA, sideB, sideC);
      double area = userTriangle.getArea();
      double perimeter = userTriangle.getPerimeter();
      JOptionPane.showMessageDialog(null, "A triangle with\n"
              + "a side \"A\" of " + unitTest(sideA) + ",\n"
              + "a side \"B\" of " + unitTest(sideB) + ",\n"
              + "a side \"C\" of " + unitTest(sideC) + ",\n"
              + "has an area of " + unitTest(area)
              + ", and a perimeter of " + unitTest(perimeter));
  }
  
  private static String unitTest(double userEntry) {
      if (userEntry == 1) {
          return userEntry + " unit";
      } else {
          return userEntry + " units";
      }
     }
  }
  </code></pre>