# Problem with the while loop

• 02-14-2010, 02:55 PM
molokomesto
Problem with the while loop
Hi, I`m new here (and to Java). Hope I can get some help. :)

So I am to write a program that takes as input a year and calculate the Gregorian epact number. For the calculation part, it`s ok.

However If the user enters a year which is not of 4-digit, your program must display an error message and continuously prompt the user for a 4-digit year until a valid year is entered.
Here is my take:
Quote:

import java.util.Scanner;
public class epact {
public static void main(String[]args){
Scanner input=new Scanner(System.in);
int year=input.nextInt();
String s = new Integer(year).toString();
int yrlength=s.length();

while (yrlength>4){
System.out.print("Only 4 digit years accepted.");
}

int c = year/100;
int e= (8 + (c/4));
int p= c + ((8*c + 13)/25);
int a= 11 * (year%19)%30;
int result=e-p+a;
System.out.print("The The Gregorian Epact is of" +result);

}
}

If I enter a valid year, it works but the problem is the looping in case of invalid year. What I want is that after each invalid year input, after the error message, the user is prompt to enter a year again. Not sure how to do this.
• 02-14-2010, 04:34 PM
Fubarable

Code:

```public static void main(String[]args){   Scanner input=new Scanner(System.in);   System.out.print("Enter a year please: ");   int year=input.nextInt();     input.nextLine();  // you'll probably need to discard the end of line token   String s = new Integer(year).toString();   int yrlength=s.length();   while (yrlength>4){       System.out.println("Only 4 digit years accepted.");       System.out.print("Enter a year please: ");       year = ....      // ... get your data here, then calc yrlength   }   int c = year/100;   int e= (8 + (c/4));   int p= c + ((8*c + 13)/25);   int a= 11 * (year%19)%30;   int result=e-p+a;   System.out.print("The The Gregorian Epact is of" +result); }```
• 02-14-2010, 04:45 PM
m00nchile
Acctually, a do while would be the preffered way of getting input, IMHO.
Code:

```//we want a number that's greater than 10 int input; Scanner s = new Scanner(System.in); do {   input = s.nextInt(); }while(input < 10);```
Of course, you could put in an explanation and prompt the user for the info, but this is the basic solution (the one we use at uni).
• 02-14-2010, 05:07 PM
Fubarable
Quote:

Originally Posted by m00nchile
Acctually, a do while would be the preffered way of getting input, IMHO.

//...

Of course, you could put in an explanation and prompt the user for the info, but this is the basic solution (the one we use at uni).

Yep, another solution. If you use this solution and want to post an error message only after bad input entered though, you'll need to add a boolean variable or some other test to see if this is the first time through the loop or not.
• 02-14-2010, 06:01 PM
m00nchile
That's true, a little hackish, but valid solution would also be an infinite loop:
Code:

```int input; Scanner s = new Scanner(System.in); while(true) {   input = s.nextInt();   if(input < 10) {         System.out.println("Input must be 10 or over");   }   else break; }```
Since we check if the input is correct within the loop, it wouldn't make sense to check it again as a termination condition.