Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

input validatin with UI

$
0
0
I am writing a program that is suppose to verify user input (only numeric and positive). Once the user enter all inputs correctly then he can click on the run report program. My problem is that i am bel to verify that the input is wrong but the program continues to be executed. I am trying to re-ask for input until the user gets it right before moving to the next part.
Here is my code: it just loops through the 4 Textfields with warning message to the user without actually asking for new input. I have tried using return after each condition but it is not working.If there is better or optimized way of doing this please point me in the right direction.

public void inputValidation(){
    //implementing a try catch block to verify that input is numeric and positive as we cannot have negative acres
	
do {
  try
         { n1 = stringToDouble(n1Text.getText());// get value entered and see if it is a number by trying to convert it to a double
         while (n1 < 0) { //If it is a number then ensure it is not negative. Restrict users from entering negative crop values
       	  JOptionPane.showMessageDialog(this,"Please Enter a Positive Number!You can't have NEGATIVE Acres","WRONG INPUT",JOptionPane.WARNING_MESSAGE);
       	  n1Text.requestFocus();//reposition the cursor in the field for new input
       	  }
         }
  
  catch(NumberFormatException f) //verify that input from n1Text is converted to a double
           //if input is not numeric we display Warning message about wrong input
          {JOptionPane.showMessageDialog(this,"Re-enter the first numbers as a NUMERIC VALUE","WRONG INPUT",JOptionPane.WARNING_MESSAGE);
          n1Text.requestFocus();//re-position cursor in the n1TextField.
          }


    try
           { n2 = stringToDouble(n2Text.getText());
           while (n2 < 0 ) { //ensuing that the user can't enter negative crop values
         	  JOptionPane.showMessageDialog(this,"Please Enter a Positive Number!You can't have NEGATIVE Acres","WRONG INPUT",JOptionPane.WARNING_MESSAGE);
         	  n2Text.requestFocus();
         	 }
           }
           
   catch(NumberFormatException f)
           {JOptionPane.showMessageDialog(this,"Re-enter the second number as a NUMERIC VALUE","WRONG INPUT",JOptionPane.WARNING_MESSAGE);
           n2Text.requestFocus();
          }

   try
           { n3 = stringToDouble(n3Text.getText());
           while (n3 < 0) { //ensuing that the user can't enter negative crop values
           	  JOptionPane.showMessageDialog(this,"Please Enter a Positive Number!You can't have NEGATIVE Acres","WRONG INPUT",JOptionPane.WARNING_MESSAGE);
           	  n3Text.requestFocus();
           	 }
           }
   catch(NumberFormatException f)
           {JOptionPane.showMessageDialog(this,"Re-enter the third number as a NUMERIC VALUE","WRONG INPUT",JOptionPane.WARNING_MESSAGE);
            n3Text.requestFocus();
          }

    try
           { n4 = stringToDouble(n4Text.getText());
           while (n4 < 0) { //ensuing that the user can't enter negative crop values
         	  JOptionPane.showMessageDialog(this,"Please Enter a Positive Number!You can't have NEGATIVE Acres","WRONG INPUT",JOptionPane.WARNING_MESSAGE);
         	  n4Text.requestFocus();
         	}
           }
   catch(NumberFormatException f)
           {JOptionPane.showMessageDialog(this,"Re-enter the fourth number as a NUMERIC VALUE","WRONG INPUT",JOptionPane.WARNING_MESSAGE);
            n4Text.requestFocus();
         }
} while(true);
}


thank you for your help.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>