Results 1 to 3 of 3

Thread: Validation

  1. #1
    Johnny2009 is offline Member
    Join Date
    Nov 2011
    Posts
    22
    Rep Power
    0

    Default Validation

    Hi all, im trying to do a little validation here. I have a switch statement that takes in a int from the user but I want to not allow any characters. I did the try catch and it did work but then my program just ends. Any thoughts?

    Java Code:
    public class MenuClass
    {
      
      static Scanner console = new Scanner(System.in);
      static String name, address, city, state, zip, school, phone, email;
      public MenuClass()
      {
      }
      
      public void mainMenu()
      {
        int numChoice;
      
        try{
        System.out.println("     MAIN MENU");
        System.out.println("1. Enter Data");
        System.out.println("2. Display Data");
        System.out.println("3. Edit Data");
        System.out.println("4. Delete Data");
        System.out.println("5. Read File");
        System.out.println("6. Save to File");
        System.out.println("7. Select Number Choice 1-7");
        numChoice = console.nextInt();
       
        switch (numChoice){
          case 1: enterData();
          break;
          case 2: display();
          break;
          case 3: edit();
          break;
          case 4: delete();
          break;
          case 5: readFile();
          break;
          case 6: save();
          break;
          default:
            System.out.println("Invalid Choice...");
            System.out.println("RESTARTING...");
            mainMenu();
            break;
        }
        }
        catch(InputMismatchException exception){
          System.out.println("Please enter valid choice");
          }
        catch(Exception exception)
        {
          exception.printStackTrace();
        }
       }
    Last edited by Johnny2009; 09-23-2012 at 10:42 PM.

  2. #2
    JavaAdviser is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: Validation

    How about catching the exception right where it happens. You could assign an initial value to numChoice, catch the exception and then it will automatically enter the default case.

  3. #3
    Johnny2009 is offline Member
    Join Date
    Nov 2011
    Posts
    22
    Rep Power
    0

    Default Re: Validation

    hmm i see what your saying, i just dont know how to do it :\. The try catch i pretty much copied and messed around with until i got it working. Not really sure the in and outs of it to make it work

Similar Threads

  1. Validation of XML against XSD
    By abhinav_sharma in forum New To Java
    Replies: 0
    Last Post: 05-02-2012, 10:18 AM
  2. XML validation
    By Onra in forum New To Java
    Replies: 0
    Last Post: 03-24-2011, 07:14 PM
  3. XML Validation
    By sehudson in forum XML
    Replies: 5
    Last Post: 03-21-2011, 01:38 PM
  4. VAlidation
    By chetna1982 in forum New To Java
    Replies: 2
    Last Post: 12-11-2008, 08:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •