Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How do I use try-catch to validate input?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How do I use try-catch to validate input?

    Hi I seem to need help here again. I am not sure how to use the try-catch method to make sure that the user inputs only numbers, and if the user inputs anything else, it will give an error and prompt the user to enter the numbers again.

    here is the part of the code where I ask for all of the numbers.
    thanks.

     public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int count = 1;
            double s1 = 0.0, s2 = 0.0, s3 = 0.0;
            double base = 0.0, height = 0.0;
            System.out.println("Welcome to the Triangle program");
     
            while(count >= 1){
     
                System.out.println("Enter side 1: ");
                s1 = sc.nextDouble();
                System.out.println("Enter side 2: ");
                s2 = sc.nextDouble();
                System.out.println("Enter side 3: ");
                s3 = sc.nextDouble();            
                System.out.println("Enter the base: ");
                base = sc.nextDouble();
                System.out.println("Enter the height: ");
                height = sc.nextDouble(); 
                                                    }


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: How do I use try-catch to validate input?

    The basic format would be like this:
     
    while(true) {
      try {
     
      // Code to evaluate goes here.
      // In other words code that could throw an exception.
     
      } catch (Exception e) {
     
      // Tell the user they did something wrong.
      // If you know the specific exception to catch the replace Exception with it.
     
      }
    }

    One more piece of information for you. When the Scanner expects a certain type of input and the Exception is caught, the Scanner will not advance to the next input until a method (one of the Scanner methods) is called to make it advance.

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: How do I use try-catch to validate input?

    What KucerakJM said is right, your looking to catch if there is a non-number input, so the exception would be the NumberFormatException.
    [quote]
     
    while(true) {
      try {
     
      // Code to evaluate goes here.
      // In other words code that could throw an exception.
     
      } catch (NumberFormatException e) {
     
      // Tell the user they did something wrong.
      // If you know the specific exception to catch the replace Exception with it.
     
      }
    }

Similar Threads

  1. How to validate username?this my register.jsp
    By surendran610 in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: August 22nd, 2011, 01:42 AM
  2. How to validate a given URL
    By HelloAll in forum AWT / Java Swing
    Replies: 2
    Last Post: March 1st, 2011, 01:25 AM
  3. Validate JSON according XSD schema
    By edrozim in forum Java Theory & Questions
    Replies: 2
    Last Post: February 23rd, 2011, 10:42 AM
  4. java validate help??
    By zyspt in forum Java Theory & Questions
    Replies: 1
    Last Post: April 16th, 2010, 02:48 AM
  5. Validate in server side..
    By Ganezan in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 27th, 2009, 06:36 AM