Results 1 to 7 of 7
- 10-18-2011, 03:17 AM #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,
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.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 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.
- 10-18-2011, 03:44 AM #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.
- 10-18-2011, 04:03 AM #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?
- 10-18-2011, 04:22 AM #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?
Did you Google "try statement"?
- 10-18-2011, 05:09 AM #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
- 10-18-2011, 05:26 AM #6
Re: How to check for errors in input and then give another chance
- 10-20-2011, 03:39 PM #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
Its a bit chunky but it does the trick :)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);Last edited by HellSpawn; 10-20-2011 at 03:42 PM.
Similar Threads
-
if wrong input, you get a new chance
By hreodbeohrt in forum New To JavaReplies: 1Last Post: 10-18-2011, 02:07 AM -
IDEA 10 give me compiler errors
By yma16 in forum IntelliJ IDEAReplies: 1Last Post: 05-01-2011, 05:07 PM -
Give Value to other class to check database
By dread_arisawa in forum New To JavaReplies: 1Last Post: 06-23-2010, 09:52 PM -
Input type Check box
By jeeva in forum Advanced JavaReplies: 1Last Post: 02-05-2009, 04:04 PM -
how to avoid input errors?
By Sinnergy in forum New To JavaReplies: 5Last Post: 02-02-2009, 11:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks