Results 1 to 4 of 4
  1. #1
    beginnerprog is offline Member
    Join Date
    Mar 2013
    Posts
    22
    Rep Power
    0

    Default Using try and catch problem

    What im doing is asking the user to input a month. ive put in a if and else if to say whether the month > 12 or <1 and reask the user to input a month. Im trying to use the try and catch statement to catch the user entering a string and tell them to enter a month. The problem with my code is that when it catches the user entering a string it command line keeps repeating the "Please enter a month 1-12" and let user re enter the month . Can anyone stop the problem i'm having trouble with.

    Java Code:
    import java.util.*;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Scanner;
     
    public class Price
    {
        public static void main (String [] args)
        {
               userInput();
                   
            }
         
     public static void userInput(){
            Scanner scan = new Scanner(System.in);
            
            int month, day, year;
            boolean i = true;
            System.out.println("Please enter a month MM: ");
            
            while(i == true)
    	    {
    			try
    			{
    				month = scan.nextInt();
    		
    			
    					if(month < 12 && month > 1)
    					{
    					System.out.println("Thank you");
    					i = false;
    					}
    					else 
    					{
    					System.out.println("Error please enter a number within 1 and 12");
    					month = scan.nextInt();                
    					}
    				}
    				
    			catch(InputMismatchException imme)
    			{ 
    				System.out.println("Please enter a month 1-12");
    			   	              
    			}
            }
      }
      }
    Last edited by beginnerprog; 03-23-2013 at 01:02 AM.

  2. #2
    beginnerprog is offline Member
    Join Date
    Mar 2013
    Posts
    22
    Rep Power
    0

    Default Re: Using try and catch problem

    Ive tryed insert the month.scan.nextInt() into the catch but i get an error im really confused.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,418
    Rep Power
    5

    Default Re: Using try and catch problem

    The InputMismatchException will only be thrown when the scanner tries to input an incorrect type. You are better off using your conditional test to verify input.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    beginnerprog is offline Member
    Join Date
    Mar 2013
    Posts
    22
    Rep Power
    0

    Default Re: Using try and catch problem

    Quote Originally Posted by jim829 View Post
    The InputMismatchException will only be thrown when the scanner tries to input an incorrect type. You are better off using your conditional test to verify input.

    Regards,
    Jim
    i have solved this problem by using scan.next()

    thanks

Similar Threads

  1. try and catch Problem ...
    By LogicBrix in forum New To Java
    Replies: 2
    Last Post: 02-05-2012, 06:22 AM
  2. try and catch problem
    By Masamune1059 in forum New To Java
    Replies: 6
    Last Post: 01-08-2012, 04:47 AM
  3. try catch problem
    By g123456 in forum New To Java
    Replies: 8
    Last Post: 05-17-2010, 06:54 PM
  4. Problem JOptionPane try catch
    By jason99 in forum New To Java
    Replies: 1
    Last Post: 07-10-2009, 10:39 PM
  5. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 10:54 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
  •