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

1. Member
Join Date
Oct 2011
Posts
4
Rep Power
0

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. 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. Member
Join Date
Oct 2011
Posts
4
Rep Power
0

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. 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?

5. Member
Join Date
Oct 2011
Posts
4
Rep Power
0

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. Re: How to check for errors in input and then give another chance

Originally Posted by HellSpawn
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. Member
Join Date
Oct 2011
Posts
4
Rep Power
0

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.

Posting Permissions

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