2

I just want to calculate your future age. You enter 3 things: your current age, the current year, and the future year. I was experiementing with for and do while loops. Then I came cross this problem, variable currentAge is not initialized. But none of the other variables are initialized either, other variables are fine. The only difference here is others are in the do while loop, but currentAge is in the for loop. Why did this happen? Can somebody explain the difference and why? Please see the below code.

import java.util.Scanner; 

public class Lab3Class 
{ 

    public static void main(String[] args) 
    { 

        Scanner input = new Scanner(System.in); 
        String cleanUpStr; 
        int currentAge; 
        int futureAge; 
        int currentYear; 
        int futureYear;

        for (int cntr = 0; cntr < 3; ++cntr) 
        { 

            System.out.print("Enter your current age\t"); 

            currentAge = input.nextInt( ); 

            cleanUpStr = input.nextLine( ); 
        }

    if (currentAge >= 0){

        do 
        { 
            System.out.print("Enter current year\t"); 

            currentYear = input.nextInt( ); 

            cleanUpStr = input.nextLine( ); 
        } while(currentYear < 0); 

        do 
        { 
            System.out.print("Enter future year\t"); 

            futureYear = input.nextInt( ); 
            cleanUpStr = input.nextLine( ); 
        } while(futureYear < 0 || futureYear < currentYear);

        input.close();
        futureAge = currentAge + (futureYear - currentYear);

        System.out.println("In the year " + currentYear + " you are " + currentAge + " years old"); 
        System.out.println("In the year " + futureYear + " you will be " + futureAge + " years old"); 
    } else {
        System.out.println("Too many tries for an valid age!");
    }

    }
}

3 Answers 3

3

The do / while loop code block is executed once (at least) no matter what the condition is. But the for loop does not follow the same tradition : The for loop checks whether the condition is valid FIRST, THEN it executes.

Execution DEMO:

For loop : (pre; condition; post)

  1. Check the condition -> If condition is true -> 2. Execute code block -> 3. Go on to next iteration

Do / While loop: do { }while(condition);

  1. Execute code block -> 2. Check condition -> If condition is true -> 3. Go on to next iteration.
Sign up to request clarification or add additional context in comments.

2 Comments

And consequently it could happen that "currentAge" has not been initialized when it is read in the if-statement.
That exaplaied my question. Thanks man!! really appreciate it!
1

Try this version of code it's perfect:

import java.util.Scanner;

public class Lab3Class {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    String cleanUpStr;
    int currentAge = 0;
    int futureAge;
    int currentYear;
    int futureYear;

    System.out.print("Enter current age\t");
    currentAge = input.nextInt();

    if (currentAge >= 0) {
        do {
            System.out.print("Enter current year\t");
            currentYear = input.nextInt();
            cleanUpStr = input.nextLine();
        } while (currentYear < 0);

        do {

            System.out.print("Enter future year\t");
            futureYear = input.nextInt();

            cleanUpStr = input.nextLine();
        } while (futureYear < 0 || futureYear < currentYear);

        input.close();
        futureAge = currentAge + (futureYear - currentYear);

        System.out.println("In the year " + currentYear + " you are "
                + currentAge + " years old");
        System.out.println("In the year " + futureYear + " you will be "
                + futureAge + " years old");
    } else {
        System.out.println("Too many tries for an valid age!");
    }

 }
}

Comments

1

In order to make it works - you just need to change currentAge declaration line setting it to initial value, like this:

int currentAge = 0;

Java initialize class fields with default values, but not methods variables. As an alternative solution (just to improve understanding) I would recommend to move declaration of currentAge to class level and leave it without default value, like following:

public class Lab3Class
{

    private static         int currentAge;
...
}

The program behavior will be the same, but will depict difference between field and method variable declaration and initialization.

You may find useful following links: http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.1

http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.