# Thread: Seconds Left to Live

1. Member
Join Date
Sep 2011
Posts
24
Rep Power
0

## Seconds Left to Live

My program is giving me the wrong answers. The programs job is the ask the user for the years, months, and days they have been alive. Then calculate how many seconds the user has been alive, and then how many seconds the user has left to live (by subtracting the average 2.5 billion average of seconds. I have already calculated all the if statements and looked over my calculations numerous times, so I don't see what it is not giving me the right output. Thanks for your help.

monthsToDays = years * 365;
monthsToDays = (monthsToDays + months);
monthsToDays = (monthsToDays + days);

secleft = monthsToDays * 24 * 60 * 60;

System.out.println("You have been alive for " + secleft + " seconds");

System.out.println("The average human life is 2,500,000,000 seconds");

System.out.printf("You have " + (averagelife - secleft) + " seconds to live !");

2. Senior Member
Join Date
Jul 2011
Location
Melbourne, Victoria, Australia
Posts
155
Rep Power
6

## Re: Seconds Left to Live

monthsToDays = years * 365;
monthsToDays = (monthsToDays + months);
monthsToDays = (monthsToDays + days);
To add variables together you need to use: '+=' and when subtracting: '-='.
Try changing your calculations to this:
Java Code:
```monthsToDays = years * 365;
monthsToDays = monthsToDays += months;
monthsToDays = monthsToDays += days;```

3. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
6

## Re: Seconds Left to Live

Originally Posted by jammas615

To add variables together you need to use: '+=' and when subtracting: '-='.
Try changing your calculations to this:
Java Code:
```monthsToDays = years * 365;
monthsToDays = monthsToDays += months;
monthsToDays = monthsToDays += days;```
That is correct but += is just a shortcut to what he's doing, his way works as well.

I see one objective here is to convert everything you get from the user (years, months, and days) to days and then add that together.

Take a look at
Java Code:
`monthsToDays = (monthsToDays + months);`
Do you see anything wrong with it relating to your objective?
Last edited by Solarsonic; 10-03-2011 at 12:59 AM.

4. Member
Join Date
Sep 2011
Posts
24
Rep Power
0

## Re: Seconds Left to Live

Originally Posted by jammas615

To add variables together you need to use: '+=' and when subtracting: '-='.
Try changing your calculations to this:
Java Code:
```monthsToDays = years * 365;
monthsToDays = monthsToDays += months;
monthsToDays = monthsToDays += days;```
Changed what you suggested and still gives me the wrong output. The output it gives me is..
Last edited by mc1234; 10-03-2011 at 03:09 AM.

5. Member
Join Date
Sep 2011
Posts
24
Rep Power
0

## Re: Seconds Left to Live

Originally Posted by Solarsonic
That is correct but += is just a shortcut to what he's doing, his way works as well.

I see one objective here is to convert everything you get from the user (years, months, and days) to days and then add that together.

Take a look at
Java Code:
`monthsToDays = (monthsToDays + months);`
Do you see anything wrong with it relating to your objective?
Should it be like this instead?

secleft = years * 365 += monthstoDays +=days;

secleft = monthsToDays * 24 * 60 * 60;

System.out.println("You have been alive for " + secleft + " seconds");

System.out.println("The average human life is 2,500,000,000 seconds");

System.out.printf("You have " + (averagelife - secleft) + " seconds to live !");

6. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
6

## Re: Seconds Left to Live

Originally Posted by mc1234
Should it be like this instead?

secleft = years * 365 += monthstoDays +=days;

secleft = monthsToDays * 24 * 60 * 60;

System.out.println("You have been alive for " + secleft + " seconds");

System.out.println("The average human life is 2,500,000,000 seconds");

System.out.printf("You have " + (averagelife - secleft) + " seconds to live !");
This is how you should get seconds lived:

Java Code:
```int totalDaysLived;
totalDaysLived += years * 365;
totalDaysLived += months * 30;
totalDaysLived += days;

int secondsLived = totalDaysLived * 24 * 60 * 60;```
Last edited by Solarsonic; 10-03-2011 at 01:35 AM.

7. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
6

## Re: Seconds Left to Live

Originally Posted by mc1234
long totalDaysLived = 0;
totalDaysLived += years * 365;
totalDaysLived += months * 30;
totalDaysLived += days;

long secondsLived = totalDaysLived *24 * 60 *60;

System.out.println("You have been alive for " + totalDaysLived + " seconds");

System.out.println("The average human life is 2,500,000,000 seconds");

System.out.printf("You have " + (averagelife - secondsLived) + " seconds to live !");

This is the output this gives...

Enter you age in years
24
3
19
You have been alive for 8959 seconds
The average human life is 2,500,000,000 seconds
You have 1725942400 seconds to live !

Should be...

Enter your age in years months and days.
Years: 24
Months: 3
Days: 19

You have been alive for 766,281,600 seconds.
Average human life is 2,500,000,000 seconds.
You have 1,733,718,400 seconds left to live!
Your code is illogical and flawed in many ways.

I advise you to re-try this from the beginning, using my code:

Java Code:
```int totalDaysLived;
totalDaysLived += years * 365;
totalDaysLived += months * 30;
totalDaysLived += days;

int secondsLived = totalDaysLived * 24 * 60 * 60;```
for obtaining seconds lived.

8. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
6

## Re: Seconds Left to Live

Originally Posted by mc1234
Changed my code, like you suggested. Here is the full code.

import java.util.*;

public class AgaininSeconds {

public static void main(String[] args)

{

Scanner keyboard = new Scanner(System.in);

Long years, months, days, averagelife, secondslived;

System.out.println("Enter you age in years");
years = keyboard.nextLong();

months = keyboard.nextLong();

days = keyboard.nextLong();

averagelife = 2500000000l;

if (months ==1)months +=31;
else if (months ==2)days +=59;
else if (months ==3)days +=90;
else if (months ==4)days +=120;
else if (months ==5)days +=151;
else if (months ==6)days +=181;
if (months ==7)days +=212;
if (months ==8)days +=243;
if (months ==9)days +=273;
if (months ==10)days +=304;
if (months ==11)days +=334;
if (months ==12)days +=365;

Long totaldayslived = (long) 0;
totaldayslived += years * 365;
totaldayslived += months * 30;
totaldayslived += days;

secondslived = totaldayslived *24 * 60 *60;

System.out.println("You have been alive for " + totaldayslived + " seconds");

System.out.println("The average human life is 2,500,000,000 seconds");

System.out.printf("You have " + (averagelife - secondslived) + " seconds to live !");

}

}

Output:

Enter you age in years
24
3
19
You have been alive for 8959 seconds
The average human life is 2,500,000,000 seconds
You have 1725942400 seconds to live !
Change

Java Code:
`Long totaldayslived = null;`
to

Java Code:
`long totaldayslived;`
Also, I see what you're trying to do with your if statements.

You can figure out the exact number of days in each month but for that you'll need to get today's date etc; your if statements are flawed.

If you're new to Java then I'm guessing your instructor expects you to just estimate 30 days per month, otherwise you'll have to work with the JDK Date class (http://download.oracle.com/javase/1....util/Date.html) and fix your if statements.

9. Member
Join Date
Sep 2011
Posts
24
Rep Power
0

## Re: Seconds Left to Live

When I changed it to this...

Long totaldayslived;
totaldayslived += years * 365;
// On the above totaldayslived it says the local variable totaldayslived may not have been initialized.
totaldayslived += months * 30;
totaldayslived += days;

Also, we are supposed to assume the different days in different months. I am not familiar with the with the Date Class you are referring to.

10. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
6

## Re: Seconds Left to Live

Originally Posted by mc1234
When I changed it to this...

Long totaldayslived;
totaldayslived += years * 365;
// On the above totaldayslived it says the local variable totaldayslived may not have been initialized.
totaldayslived += months * 30;
totaldayslived += days;

Also, we are supposed to assume the different days in different months. I am not familiar with the with the Date Class you are referring to.
Well you can only initialize a variable like

Java Code:
`long totaldayslived;`
inside of a static method or outside any methods.

Change it to

Java Code:
`long totaldayslived - 0L;`
As for the different days in different months, you won't know which months they've lived unless you know today's date correct?

If today was December 7th, you would get different months that he lived than if it was October 2nd.
Last edited by Solarsonic; 10-03-2011 at 02:15 AM.

11. Member
Join Date
Sep 2011
Posts
24
Rep Power
0

## Re: Seconds Left to Live

Originally Posted by Solarsonic
Well you can only initialize a variable like

Java Code:
`long totaldayslived;`
inside of a static method or outside any methods.

Change it to

Java Code:
`long totaldayslived - 0L;`
As for the different days in different months, you won't know which months they've lived unless you know today's date correct?

If today was December 7th, you would get different months that he lived than if it was October 2nd.

These are part of the instructions given...

Write a Java application program that will ask the user for their age in years, months, and days. The program will then calculate and display the user's age in seconds.

Begin by asking the user to enter their age in years, months and days.

After the age has been entered, calculate the number of seconds the user has been alive.

ect..

12. Member
Join Date
Sep 2011
Posts
24
Rep Power
0

## Re: Seconds Left to Live

More instructions..

You must, however, correctly take into account the different number of days in different months. For example, if the user enters 5 for the number of months, you would add up the number of days in the first 5 months: 31+28+31+30+31

13. Member
Join Date
Sep 2011
Posts
24
Rep Power
0

## Re: Seconds Left to Live

I am a beginner, but we are supposed to take into account the different days in different months, but can ignore leap years.

"You must, however, correctly take into account the different number of days in different months. For example, if the user enters 5 for the number of months, you would add up the number of days in the first 5 months: 31+28+31+30+31"

14. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
6

## Re: Seconds Left to Live

Originally Posted by mc1234
I am a beginner, but we are supposed to take into account the different days in different months, but can ignore leap years.

"You must, however, correctly take into account the different number of days in different months. For example, if the user enters 5 for the number of months, you would add up the number of days in the first 5 months: 31+28+31+30+31"
Ok so what do you get now, changing

Java Code:
`long totaldayslived;`
to
Java Code:
`long totaldayslived = 0L;`

15. Member
Join Date
Sep 2011
Posts
24
Rep Power
0

## Re: Seconds Left to Live

Yes I changed that. Thank you. Still so confused about this though. I just don't get why its not giving out the correct output. Been looking at it for hours! Yikes!

16. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
6

## Re: Seconds Left to Live

Also I see that you can just erase

Java Code:
`totaldayslived += months * 30;`

Btw next time please post all of your code because I could have spotted this earlier on.
Last edited by Solarsonic; 10-03-2011 at 02:37 AM.

17. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
6

## Re: Seconds Left to Live

Originally Posted by mc1234
import java.util.*;
import java.util.Date;

public class AgaininSeconds {

public static void main(String[] args)

{

Scanner keyboard = new Scanner(System.in);

Long years, months, days, averagelife, secondslived, monthstodays;

System.out.println("Enter you age in years");
years = keyboard.nextLong();

months = keyboard.nextLong();

days = keyboard.nextLong();

averagelife = 2500000000l;

if (months ==1)
{
monthstodays = (31L);
}
else if (months ==2)
{
monthstodays = (59L);
}

else if (months ==3)
{
monthstodays = (90L);
}
else if (months ==4)
{
monthstodays = (120L);
}
else if (months ==5)
{
monthstodays = (151L);
}
else if (months ==6)
{
monthstodays = (181L);
}
if (months ==7)
{
monthstodays = (212L);
}
if (months ==8)
{
monthstodays = (243L);
}
if (months ==9)
{
monthstodays = (273L);
}
if (months == 10)
{
monthstodays = (304L);
}
if (months ==11)
{
monthstodays = (334L);
}
if (months ==12)
{
monthstodays = (365L);
}

Long totaldayslived = 0L;
totaldayslived += years * 365;
totaldayslived += days;

secondslived = totaldayslived *24 * 60 *60;

System.out.println("You have been alive for " + secondslived + " seconds");

System.out.println("The average human life is 2,500,000,000 seconds");

System.out.printf("You have " + (averagelife - secondslived) + " seconds to live!");

}

}

Still giving incorrect output of..

You have been alive for 758505600 seconds
The average human life is 2,500,000,000 seconds
You have 1741494400 seconds to live!

Change

Java Code:
```if (months ==1)
{
monthstodays = (31L);
}
else if (months ==2)
{
monthstodays = (59L);
}

else if (months ==3)
{
monthstodays = (90L);
}
else if (months ==4)
{
monthstodays = (120L);
}
else if (months ==5)
{
monthstodays = (151L);
}
else if (months ==6)
{
monthstodays = (181L);
}
if (months ==7)
{
monthstodays = (212L);
}
if (months ==8)
{
monthstodays = (243L);
}
if (months ==9)
{
monthstodays = (273L);
}
if (months == 10)
{
monthstodays = (304L);
}
if (months ==11)
{
monthstodays = (334L);
}
if (months ==12)
{
monthstodays = (365L);
}```
to

Java Code:
```if (months ==1)
{
days += (31L);
}
else if (months ==2)
{
days += (59L);
}

else if (months ==3)
{
days += (90L);
}
else if (months ==4)
{
days += (120L);
}
else if (months ==5)
{
days += (151L);
}
else if (months ==6)
{
days += (181L);
}
if (months ==7)
{
days += (212L);
}
if (months ==8)
{
days += (243L);
}
if (months ==9)
{
days += (273L);
}
if (months == 10)
{
days += (304L);
}
if (months ==11)
{
days += (334L);
}
if (months ==12)
{
days += (365L);
}```
Your program will give you the correct output.

Also I don't know if you've learned about switch statements yet, but using them would be better than having so many if statements.

18. Member
Join Date
Sep 2011
Posts
24
Rep Power
0

## Re: Seconds Left to Live

Originally Posted by Solarsonic
Change

Java Code:
```if (months ==1)
{
monthstodays = (31L);
}
else if (months ==2)
{
monthstodays = (59L);
}

else if (months ==3)
{
monthstodays = (90L);
}
else if (months ==4)
{
monthstodays = (120L);
}
else if (months ==5)
{
monthstodays = (151L);
}
else if (months ==6)
{
monthstodays = (181L);
}
if (months ==7)
{
monthstodays = (212L);
}
if (months ==8)
{
monthstodays = (243L);
}
if (months ==9)
{
monthstodays = (273L);
}
if (months == 10)
{
monthstodays = (304L);
}
if (months ==11)
{
monthstodays = (334L);
}
if (months ==12)
{
monthstodays = (365L);
}```
to

Java Code:
```if (months ==1)
{
days += (31L);
}
else if (months ==2)
{
days += (59L);
}

else if (months ==3)
{
days += (90L);
}
else if (months ==4)
{
days += (120L);
}
else if (months ==5)
{
days += (151L);
}
else if (months ==6)
{
days += (181L);
}
if (months ==7)
{
days += (212L);
}
if (months ==8)
{
days += (243L);
}
if (months ==9)
{
days += (273L);
}
if (months == 10)
{
days += (304L);
}
if (months ==11)
{
days += (334L);
}
if (months ==12)
{
days += (365L);
}```
Your program will give you the correct output.

Also I don't know if you've learned about switch statements yet, but using them would be better than having so many if statements.

Thank you soo much!! One more thing. So I have to have commas in my print statements. In the first statement, I figured out how to correctly add the commas, but am having trouble since the last one.

System.out.printf(" You have been alive for %,d seconds.\n", secondslived);

System.out.println("The average human life is 2,500,000,000 seconds");

System.out.printf("You have" + (averagelife - secondslived) + " seconds to live!");

19. Member
Join Date
Sep 2011
Posts
24
Rep Power
0

## Re: Seconds Left to Live

Thanks so much!! One last thing. I have to add commas in the print statements. I figured out how to correctly add commas in the first print statement, but can't correctly figure it out for the last print statement.

System.out.printf("You have been alive for %,d seconds.\n", secondslived);

System.out.println("The average human life is 2,500,000,000 seconds");

System.out.printf("You have " + (averagelife - secondslived) + " seconds to live!");

20. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
6

## Re: Seconds Left to Live

Originally Posted by mc1234
Thank you soo much!! One more thing. So I have to have commas in my print statements. In the first statement, I figured out how to correctly add the commas, but am having trouble since the last one.

System.out.printf(" You have been alive for %,d seconds.\n", secondslived);

System.out.println("The average human life is 2,500,000,000 seconds");

System.out.printf("You have" + (averagelife - secondslived) + " seconds to live!");
I honestly never use printf(), since I find println() much easier.

If I were doing this then I would change

Java Code:
```System.out.printf(" You have been alive for %,d seconds.\n", secondslived);
System.out.println("The average human life is 2,500,000,000 seconds");
System.out.printf("You have" + (averagelife - secondslived) + " seconds to live!");```
to

Java Code:
```System.out.println("You have been alive for "+secondslived+" seconds.");
System.out.println("The average human life is 2,500,000,000 seconds");
System.out.println("You have "+(averagelife - secondslived)+" seconds to live!");```
Last edited by Solarsonic; 10-03-2011 at 03:13 AM.

Page 1 of 2 12 Last

#### Posting Permissions

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