Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default Verifying of Month

    Hi

    In the following code; I ask the user to input the month then the program should verify if the input is one of the 12 months or not, if not the program gives him 3 chances to input the right month other than the program will stop.

    Java Code:
    System.out.print("Month = ");
    		Month = scan.next();
    		
    		String MonArr [] = {"Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec"};
    		int j;
    		for (j=0; j<2; j++){
    			for(int i=0; i<MonArr.length ; i++){
    				if (Month.equals(MonArr[i])){
    					i = MonArr.length + 1;
    					j=3;
    				}
    			}
    			if(j<2){
    			System.out.print("Month = ");
    			Month = scan.next();
    			}
    			
    		}
    		if (j==2){System.exit(0);}
    			
    		System.out.print("Day = ");
    		Day = scan.nextInt();
    The problem is:

    When the user enters the wrong month 2 times then enters the right month correctly, the program stops !!

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    It's because it's asking for the month at the end of the loop. Basically here's what you're doing:
    Java Code:
    Ask for month
    Check month
    If false, ask for month
    Check month
    If false, ask for month
    End loop

    What you'll want to do is move the "Month=" part above your "i" for loop. So it's like this:
    Java Code:
    Loop x3:
       Ask for month
       Check month

    See what I mean?

  3. #3
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default

    Did you figure it out yet??

    Follow your loops and logic:
    ask for input:
    User types wrong input
    Inside J-loop: j=0, forloop to look for match - no match, if j<2 ask for input:
    User types wrong input
    Still inside J-loop: j=1, forloop to look for match - no match, if j<2 ask for input:
    User types Correct input
    j=2 out of J-loop does not look for match and instead does a system quit because j=2


    Hope this helps! :)

Similar Threads

  1. get the value from a table in a month range
    By javastuden in forum JDBC
    Replies: 1
    Last Post: 02-24-2010, 08:53 AM
  2. How to count no of sundays in month
    By chiragkini in forum New To Java
    Replies: 3
    Last Post: 12-25-2009, 04:34 AM
  3. Verifying existence of a table in a db
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-14-2008, 10:49 AM
  4. Current month
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-07-2008, 09:40 AM
  5. date one month back
    By senzacionale in forum New To Java
    Replies: 1
    Last Post: 12-24-2007, 10:02 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
  •