Results 1 to 11 of 11

Thread: Hey

  1. #1
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default Hey

    Hey there guys. I'm new to java and i am trying to create a program that prints out lines which say how many years of working you have left from a given year as command line argument. I have the following code, but it keeps displaying lines starting with the birthyear instead of the present year you have typed. The output should be:

    You have 'yearsLeftWorking' years left.
    In 'inSomeYear' you will have 'yearsLeftWorking' years left to work.
    ....
    ...
    ...
    and so on each time depreciating the number of years working and increasing the year.

    public class WorkFuture
    {
    public static void main(String [] args)
    {

    int retiringAge = 68;


    int presentYear = Integer.parseInt(args[0]);


    int birthYear = Integer.parseInt(args[1]);

    int yearsLeftWorking = (retiringAge - (presentYear - birthYear));

    System.out.println("You have " + yearsLeftWorking + " years left to work");

    int inSomeYear = birthYear + 1;
    int ageInSomeYear = inSomeYear - birthYear;
    int retiringYear = birthYear + retiringAge;
    for (int i=presentYear; i < retiringYear; presentYear++)

    inSomeYear=inSomeYear + 1;
    yearsLeftWorking = yearsLeftWorking - 1;
    System.out.println("In " + inSomeYear + " you will have " + yearsLeftWorking + " years to work");


    Any help?

    Kind regards

    Shyam

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Set up your for loop to begin at 0 and exit when greater than yearsLeftWorking.
    say your for loop began like
    Java Code:
    for(int i=0;i<yearsLeftWorking;i++)
    Now all you have to do is add i to presentYear(to get current year now) and subtract i from yearsLeftWorking(to get years left to work) through each iteration through the for loop

  3. #3
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    Hi.

    it doesn't seem to work when you add i to presentYear and subtract from yearsLeftWorking as it skips out some years for some reason, but it works when u add 1 and subtract 1. But it seems to stop at "You have 24 years left to work" .. :S

    Thanks

    Kind Regards

    Shyam

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Can I see what you have coded?

  5. #5
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    Sure,

    int retiringYear = birthYear + retiringAge;
    for (int i=0; i < yearsLeftWorking; i++)
    {
    presentYear=presentYear + 1;
    yearsLeftWorking = yearsLeftWorking - 1;
    System.out.println("In " + presentYear + " you will have " + yearsLeftWorking + " years to work");

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Sorry I should have been more specific do the subtraction/addition inside the System.out statement

    Java Code:
    for (int i=0; i < yearsLeftWorking; i++)
    {
    System.out.println("In " + (presentYear+i) + " you will have " + (yearsLeftWorking-i) + " years to work"); 
    }
    Last edited by al_Marshy_1981; 10-27-2010 at 03:45 PM. Reason: dang curly brace

  7. #7
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    Ohhhh works finally! lol Thanks man. I'm just new to this, and i'm clearly not GOOD at it haha!

  8. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    No problem, go through it all again, make sure you understand what is happening inside that final System.out statement. Just keep practicing.

  9. #9
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    Cool! Thanks a lot mate.

    Shyam

  10. #10
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    You there? I need more help im afraid :(

    If its not too much trouble.

    Shyam

    Kind regards

  11. #11
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    If it is related to this problem post here if not start a new thread (with a descriptive title)

Posting Permissions

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