Results 1 to 7 of 7
  1. #1
    HellSpawn is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default How to check for errors in input and then give another chance

    In this program we were assigned to write out a program that displays Zellers Congruence, we were then given back the program and asked to "idiot-proof" it. So for instance if I ask for a day from 1-31 and the input is KFC, it should tell the user to input a new number from 1-31.
    Here is my code,


    Java Code:
    import java.util.Scanner;
    public class ZellersCongruence {
    
    	/**
    	 * @param args
    	 */
    	
    	public static void main(String[] args) 
    	{
    	Scanner UserInput = new Scanner(System.in);
    		//Declare array of days of the week so that I can call on them with the result of the congruence formula
    		 String[] Days =
    			{
    				"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
    			};
    	
    	//Prompt user for date month then year
    
    		System.out.print("Enter the day of the month (1-31): ");
    			 String d = UserInput.next();
    			 int dVar = Integer.parseInt(d);
    			do
    			{
    				if(dVar < 1 || dVar > 31)
    				{
    					System.out.print("That day is invalid, please enter a day between 1 and 31");
    					dVar = UserInput.nextInt();
    				}
    			}while(dVar < 1 || dVar > 31);
    		System.out.print("Enter the month (1-12): ");
    			String m = UserInput.next();
    			int mVar = Integer.parseInt(m);
    			do
    			{
    				if(mVar < 1 || mVar > 12)
    				{
    					System.out.print("That month is invalid, please enter a day between 1 and 12");
    					mVar = UserInput.nextInt();
    				}
    			}while(mVar < 1 || mVar > 31);
    		System.out.print("Enter the year (yyyy)");
    			int y = UserInput.nextInt();
    			//If mVar = 1 or 2, add twelve to the month and go to the previous year		
    			if (mVar < 3)
    			{
    				mVar = mVar + 12;
    				y = y - 1;
    			}	
    			
    
    	//Define K and J as they are defined in the formula
    			int k = y % 100;
    			int j = y / 100;
    
    	//Plug in Formula
    	int day = ((dVar + ((26*(mVar+1))/10) + k + (k/4) + (j/4) + (5*j))%7);
    		System.out.println("That day was a " + Days[day]);
    					
    
    	}
    
    }
    I have it checking if its the right integer, but I want to make it that if it is not an Int, instead of throwing an IllegalMismatchException, it just prompts again.

    I figure it is something with a try/catch block but I have no idea how to use that and everything I've been trying has not been working.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: How to check for errors in input and then give another chance

    My first advice is do not cram all your code into the main method. Break it down into smaller chunks and put each chunk into its own method. The code already handles values outside the range so all you need to do is worry about when user input is not a valid integer. Easiest way is to place the Integer.parseInt call inside a try statement and catch the Exception.

  3. #3
    HellSpawn is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: How to check for errors in input and then give another chance

    Right; thats what I have been told, but I am currently in an Intro to Java course and this is my first try really with the language and I cannot figure out for the life of me how to use the try and catch block.

    How would I go about using it if i wanted to catch the NumberFormatException?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: How to check for errors in input and then give another chance

    What have you tried?
    Did you look up "try statement" in your text book?
    Did you Google "try statement"?

  5. #5
    HellSpawn is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: How to check for errors in input and then give another chance

    I found it on google and tried to apply it but couldn't.

    I was trying
    try
    {

    System.out.print("Please enter m (Integer from 1-12)");
    int m = UserInput.nextInt();

    }catch(NumberFormatException exception)
    {
    System.out.print("The day is invalid");

    }
    but it doesn't resolve m to a variable out of the block...
    (I know this doesn't handle the after 12 or before 12, but I am trying to get this to work first)

    I was thinking of using if(input.hasNextInt()) to go through my input and make sure its an integer but I can'tfigure out how to use it and link it to m

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: How to check for errors in input and then give another chance

    Quote Originally Posted by HellSpawn View Post
    but it doesn't resolve m to a variable out of the block...
    You have a scope issue. Since you declared the variable inside the try statement you can only use it inside the try statement. If you want to use it outside the try statement then you need to declare it outside the try statement.

  7. #7
    HellSpawn is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: How to check for errors in input and then give another chance

    I ended up solving it using hasNextInt

    Java Code:
    boolean validInt = true;
    boolean validRange = false;
    do
    {
            do
            {
            //Prompt user for date
            
                    System.out.print("Enter a date from 1-31");
     
                            if(UserInput.hasNextInt()) //Is the input an integer? If yes goto 1, if no goto 2
                                    {
     
                                            d = UserInput.nextInt();
                                            validInt = true;
                                    }
                            else
                                    {
                                            System.out.print("Please enter the day from 1-31, not in letters.\n "); //2) Prompt user for new input
                                            validInt = false;
                                            UserInput.nextLine();
                                    
                                    }
     
            }while(validInt == false);
     
            if(d < 1 || d > 31) //1) Is the integer in the right range?
            {
                    UserInput.nextLine();
                    System.out.print("That day is invalid, please enter a day between 1 and 31\n");
                    validRange = false;
                    
            }
            else validRange = true;
    }while(validInt == false || validRange == false);
    Its a bit chunky but it does the trick :)
    Last edited by HellSpawn; 10-20-2011 at 03:42 PM.

Similar Threads

  1. if wrong input, you get a new chance
    By hreodbeohrt in forum New To Java
    Replies: 1
    Last Post: 10-18-2011, 02:07 AM
  2. IDEA 10 give me compiler errors
    By yma16 in forum IntelliJ IDEA
    Replies: 1
    Last Post: 05-01-2011, 05:07 PM
  3. Give Value to other class to check database
    By dread_arisawa in forum New To Java
    Replies: 1
    Last Post: 06-23-2010, 09:52 PM
  4. Input type Check box
    By jeeva in forum Advanced Java
    Replies: 1
    Last Post: 02-05-2009, 04:04 PM
  5. how to avoid input errors?
    By Sinnergy in forum New To Java
    Replies: 5
    Last Post: 02-02-2009, 11:25 PM

Posting Permissions

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