# Seconds Left to Live

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-02-2011, 08:37 PM
mc1234
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 !");
• 10-03-2011, 12:46 AM
jammas615
Re: Seconds Left to Live
Quote:

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:
Code:

```monthsToDays = years * 365; monthsToDays = monthsToDays += months; monthsToDays = monthsToDays += days;```
• 10-03-2011, 12:52 AM
Solarsonic
Re: Seconds Left to Live
Quote:

Originally Posted by jammas615

To add variables together you need to use: '+=' and when subtracting: '-='.
Try changing your calculations to this:
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
Code:

`monthsToDays = (monthsToDays + months);`
Do you see anything wrong with it relating to your objective?
• 10-03-2011, 01:21 AM
mc1234
Re: Seconds Left to Live
Quote:

Originally Posted by jammas615

To add variables together you need to use: '+=' and when subtracting: '-='.
Try changing your calculations to this:
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..
• 10-03-2011, 01:30 AM
mc1234
Re: Seconds Left to Live
Quote:

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
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 !");
• 10-03-2011, 01:33 AM
Solarsonic
Re: Seconds Left to Live
Quote:

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:

Code:

```int totalDaysLived; totalDaysLived += years * 365; totalDaysLived += months * 30; totalDaysLived += days; int secondsLived = totalDaysLived * 24 * 60 * 60;```
• 10-03-2011, 01:46 AM
Solarsonic
Re: Seconds Left to Live
Quote:

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:

Code:

```int totalDaysLived; totalDaysLived += years * 365; totalDaysLived += months * 30; totalDaysLived += days;   int secondsLived = totalDaysLived * 24 * 60 * 60;```
for obtaining seconds lived.
• 10-03-2011, 02:01 AM
Solarsonic
Re: Seconds Left to Live
Quote:

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

Code:

`Long totaldayslived = null;`
to

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.
• 10-03-2011, 02:06 AM
mc1234
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-03-2011, 02:11 AM
Solarsonic
Re: Seconds Left to Live
Quote:

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

Code:

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

Change it to

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.
• 10-03-2011, 02:16 AM
mc1234
Re: Seconds Left to Live
Quote:

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

Code:

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

Change it to

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..
• 10-03-2011, 02:22 AM
mc1234
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
• 10-03-2011, 02:25 AM
mc1234
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"
• 10-03-2011, 02:28 AM
Solarsonic
Re: Seconds Left to Live
Quote:

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

Code:

`long totaldayslived;`
to
Code:

`long totaldayslived = 0L;`
• 10-03-2011, 02:29 AM
mc1234
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!
• 10-03-2011, 02:30 AM
Solarsonic
Re: Seconds Left to Live
Also I see that you can just erase

Code:

`totaldayslived += months * 30;`

Btw next time please post all of your code because I could have spotted this earlier on.
• 10-03-2011, 02:58 AM
Solarsonic
Re: Seconds Left to Live
Quote:

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

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

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.
• 10-03-2011, 03:04 AM
mc1234
Re: Seconds Left to Live
Quote:

Originally Posted by Solarsonic
Change

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

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!");
• 10-03-2011, 03:07 AM
mc1234
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!");
• 10-03-2011, 03:11 AM
Solarsonic
Re: Seconds Left to Live
Quote:

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

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

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!");```
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last