Results 1 to 11 of 11
Thread: Hey
- 10-26-2010, 09:47 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
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
- 10-26-2010, 10:00 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Set up your for loop to begin at 0 and exit when greater than yearsLeftWorking.
say your for loop began like
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 loopJava Code:for(int i=0;i<yearsLeftWorking;i++)
- 10-27-2010, 03:29 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
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
- 10-27-2010, 03:32 PM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Can I see what you have coded?
- 10-27-2010, 03:35 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
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");
- 10-27-2010, 03:45 PM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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
- 10-27-2010, 03:57 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
Ohhhh works finally! lol Thanks man. I'm just new to this, and i'm clearly not GOOD at it haha!
- 10-27-2010, 04:02 PM #8
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
No problem, go through it all again, make sure you understand what is happening inside that final System.out statement. Just keep practicing.
- 10-27-2010, 04:16 PM #9
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
Cool! Thanks a lot mate.
Shyam
- 10-27-2010, 06:13 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
You there? I need more help im afraid :(
If its not too much trouble.
Shyam
Kind regards
- 10-27-2010, 07:04 PM #11
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks