Results 1 to 5 of 5
  1. #1
    molokomesto is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default 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:
    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);

    }
    }
    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.
    Thanks in advance.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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);
    }

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Acctually, a do while would be the preffered way of getting input, IMHO.
    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);
    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).

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by m00nchile View Post
    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.

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    That's true, a little hackish, but valid solution would also be an infinite loop:
    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;
    }
    Since we check if the input is correct within the loop, it wouldn't make sense to check it again as a termination condition.

Similar Threads

  1. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  2. Problem with loop
    By Tykk in forum New To Java
    Replies: 7
    Last Post: 10-04-2009, 10:26 PM
  3. if else loop problem
    By Ms.Ranjan in forum New To Java
    Replies: 12
    Last Post: 04-25-2009, 09:30 AM
  4. Loop Problem
    By jralexander in forum New To Java
    Replies: 4
    Last Post: 12-02-2008, 07:08 AM
  5. For loop problem
    By mcal in forum New To Java
    Replies: 32
    Last Post: 01-25-2008, 03:51 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
  •