Results 1 to 5 of 5
Thread: Problem with the while loop
- 02-14-2010, 01:55 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 1
- Rep Power
- 0
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:
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.import java.util.Scanner;
public class epact {
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.print("Enter a year please: ");
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);
}
}
Thanks in advance.
-
see comments:
Java 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, 03:45 PM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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).Java 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);
-
- 02-14-2010, 05:01 PM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
That's true, a little hackish, but valid solution would also be an infinite loop:
Since we check if the input is correct within the loop, it wouldn't make sense to check it again as a termination condition.Java 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; }
Similar Threads
-
simple line problem / for loop problem
By helpisontheway in forum New To JavaReplies: 1Last Post: 11-17-2009, 06:12 AM -
Problem with loop
By Tykk in forum New To JavaReplies: 7Last Post: 10-04-2009, 10:26 PM -
if else loop problem
By Ms.Ranjan in forum New To JavaReplies: 12Last Post: 04-25-2009, 09:30 AM -
Loop Problem
By jralexander in forum New To JavaReplies: 4Last Post: 12-02-2008, 07:08 AM -
For loop problem
By mcal in forum New To JavaReplies: 32Last Post: 01-25-2008, 03:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks