Results 1 to 8 of 8

Thread: Loop error

  1. #1
    Piyiyo's Avatar
    Piyiyo is offline Member
    Join Date
    Jul 2016
    Location
    Madrid, Spain
    Posts
    6
    Rep Power
    0

    Default Loop error

    Hello everyone!

    I'm new to the forum and I am starting to learn Java using netbeans (ultra beginner ).
    I tried to make my first "attempt" to code a very small and simple program using what i have learnt in the first lessons.
    When the user introduces a valid date everything works fine, but if the date is wrong then the loop will continue running even if the user types a valid date.
    Please, if someone could help me to clarify what im doing wrong it would be very appreciated.

    Thank you in advance for your time and patience!

    The code:

    Java Code:
    package ejercicionombreyedad;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    
    public class EjercicioNombreYEdad {
    
       
        public static void main(String[] args) throws Exception {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            
            int d, m, a; //d for day, m for month and a for year
            int days=0;
            boolean fcorr=true;
            
            
            System.out.println("Please, introduce your birthday: ");
            System.out.print("Day: ");
            d = Integer.parseInt(br.readLine());
            System.out.print("Month: ");
            m = Integer.parseInt(br.readLine());
            System.out.print("Year: ");
            a = Integer.parseInt(br.readLine());
            
            switch (m) {
                case 2:
                    days = 28;
                    break;
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    days = 31;
                    break; 
                default:
                    days = 30;
                    break;
            }
                
            
            if (a==0 || d<1 || d>days || m<1 || m>12)
                fcorr=false;
            else
                fcorr=true;
            
            while (fcorr == false) {
    
                System.out.println("The date you have entered is wrong. Please, introduce a valid date.");
                System.out.print("Day: ");
                d = Integer.parseInt(br.readLine());
                System.out.print("Month: ");
                m = Integer.parseInt(br.readLine());
                System.out.print("Year: ");
                a = Integer.parseInt(br.readLine());
            
            }
            
            
            if (fcorr == true)
                System.out.println("Date of Birthday: Day: " + d + " Month: " + m + " Year: " +a);
                
        }
        
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Loop error

    Look at your while loop in line 51. fcorr is apparently false to enter the loop. But the loop will never exit unless you
    set fcorr to true or employ a break somewhere. So you need to revalidate the user input to set the proper loop condition.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Loop error

    It looks like the while loop should begin at line 18 to include
    get the date
    test the date
    if a good date - exit the loop
    if a bad date, print a message and go to top of loop to try again
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Piyiyo's Avatar
    Piyiyo is offline Member
    Join Date
    Jul 2016
    Location
    Madrid, Spain
    Posts
    6
    Rep Power
    0

    Default Re: Loop error

    Thank you Jim, i see the error. FIXED!

    Norm, thank you for your tip. A way to shorten the code got it!

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Loop error

    Two comments:
    Java Code:
            while (fcorr == false) {
    Java Code:
            if (fcorr == true)
    'fcorr' is a boolean already, so you don't need to use '== false/true'. It's also bad practice, and prone to error. If you accidentally use '=' instead of '==' you'll get incorrect results:
    Java Code:
    boolean bool = true;
    if (bool = false) {
        System.out.println("I'm false!");
    } else {
        System.out.println("I'm true!");
    }
    That would result in "I'm false!" being printed...which is not what you probably want.

    With booleans you should just use <boolean name> if you want to check if it's true, or use !<boolean name> for false.

    Java Code:
    if (fcorr) {
    }
    Java Code:
    while (!fcorr) {
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Loop error

    Quote Originally Posted by Tolls View Post
    Java Code:
    boolean bool = true;
    if (bool = false) {
        System.out.println("I'm false!");
    } else {
        System.out.println("I'm true!");
    }
    That would result in "I'm false!" being printed...which is not what you probably want.
    Yeah, right, and my name is Queen Victoria.

    kind regards,

    Queen Victoria aka Loretta
    Build a wall around Donald Trump; I'll pay for it.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Loop error

    Quote Originally Posted by JosAH View Post
    Yeah, right, and my name is Queen Victoria.

    kind regards,

    Queen Victoria aka Loretta
    All hail the queen!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Loop error

    Quote Originally Posted by gimbal2 View Post
    All hail the queen!
    Thank you, you simple, smelly peasant.

    kindest royal regards,

    Queen Victoria The Greatest
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. while loop error
    By AyuzawaM in forum New To Java
    Replies: 7
    Last Post: 08-22-2014, 09:50 AM
  2. Do While loop error
    By JeffMCincy in forum JCreator
    Replies: 1
    Last Post: 02-21-2011, 05:54 PM
  3. Loop error
    By Spyderpig in forum New To Java
    Replies: 4
    Last Post: 02-19-2011, 12:56 AM
  4. For loop error
    By ShotGunRockets in forum New To Java
    Replies: 6
    Last Post: 04-06-2009, 01:14 AM
  5. help error in while loop
    By iPetey in forum New To Java
    Replies: 3
    Last Post: 04-03-2009, 03:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •