Results 1 to 3 of 3
Thread: Validation
- 09-23-2012, 09:36 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
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 09:42 PM.
- 09-23-2012, 09:49 PM #2
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
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.
- 09-23-2012, 11:11 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
Validation of XML against XSD
By abhinav_sharma in forum New To JavaReplies: 0Last Post: 05-02-2012, 09:18 AM -
XML validation
By Onra in forum New To JavaReplies: 0Last Post: 03-24-2011, 06:14 PM -
XML Validation
By sehudson in forum XMLReplies: 5Last Post: 03-21-2011, 12:38 PM -
VAlidation
By chetna1982 in forum New To JavaReplies: 2Last Post: 12-11-2008, 07:08 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks