# if statements in JAVA

• 09-28-2011, 05:58 PM
mc1234
if statements in JAVA
I need help!! I don't get how if statements would work in this program? Can someone please explain?
• 09-28-2011, 06:02 PM
KevinWorkman
Re: if statements in JAVA
So you want us to do your homework for you? Let me get right on that...

If you have an actual question, post an SSCCE that demonstrates where you're stuck.
• 09-28-2011, 09:11 PM
mc1234
Re: if statements in JAVA
not trying to get anyone to write the code for me. just don't understand how I would use multiple if statements to test how many extra days to add to a running total of days. sorry to offend you.
• 09-28-2011, 09:13 PM
Fubarable
Re: if statements in JAVA
Quote:

Originally Posted by mc1234
not trying to get anyone to write the code for me. just don't understand how I would use multiple if statements to test how many extra days to add to a running total of days. sorry to offend you.

You're offending no one, but no one is likely going to want to help when all you've done is post your assignment verbatim and show none of the fruits of your labor. I suggest that you try to solve it first and then show us what you've done. You've nothing to lose and a lot to gain.

By the way, first solve how you'd figure this out on paper, and then work on trying to code it into Java. I'm not sure that you're going to see a lot of if blocks, but rather just a few.
• 09-28-2011, 10:01 PM
mc1234
Re: if statements in JAVA
thanks for the advice.
here is my pseudo code

Ask user for input of their age in years, months, and days. Calculate that into seconds.
Print that onto screen.
Print the average human life in seconds onto screen.
Subtract seconds user been alive minus the average human life.
Print that onto screen.

I have done the if statements for the months. Now just trying to figure out how to set up a calculation that correctly calculates years, months, and days into seconds.
• 09-28-2011, 10:12 PM
Iron Lion
Re: if statements in JAVA
How many seconds are there in a minute? How many minutes in an hour, and so forth? It's just a matter of simple multiplication.
• 09-28-2011, 10:14 PM
mc1234
Re: if statements in JAVA
what i have so far. im having problems with the commented line because i'm trying to multiply a string variable and a long variable. How can i fix this? Thanks.

System.out.println ("You have been alive for:");
seconds = (years * secondsinyear + months * secondsperday + days * secondsperday); // having problems with this li
• 09-28-2011, 10:20 PM
Iron Lion
Re: if statements in JAVA
You can't do arithmetic operations on strings. You'll have to use methods like Integer.parseInt(years) instead - or, better yet, dispense with the strings entirely:
Code:

int years = kbd.nextInt();
• 09-28-2011, 10:26 PM
mc1234
Re: if statements in JAVA
But wont the added up seconds be more that the int variable can hold?
• 09-28-2011, 10:33 PM
Iron Lion
Re: if statements in JAVA
Possibly. You can use nextLong() if you prefer, or cast it to long later.
• 09-29-2011, 08:30 PM
mc1234
Re: if statements in JAVA
Long humanlife = (long) 2,500,000,000;
// having problems with the number. keeps saying VariableDeclarator expected instead

System.out.println ("You have this much longer to live");
System.out.println (2,500,000,000 - beenalive);
// last println statements not working: "The method println(int) in the type PrintStream is not applicable for the arguments (int, int, int, long)"

// why would it be doing this? Thanks!
• 09-29-2011, 08:32 PM
Fubarable
Re: if statements in JAVA
Long != long

just like
Double != double.

Also, you can't have commas in an int or long literal.
• 09-29-2011, 08:36 PM
Iron Lion
Re: if statements in JAVA
Don't use commas to separate thousands. Java interprets this as 4 integers.

Also, casting 2500000000 to long won't work, since you're still using an out-of range int literal. To specify a long literal, append an L to the end: 2500000000L.

If you're using Java SE 7 and want to separate your literals into thousands (or any other way), you can use underscores:
Code:

long humanLife = 2_500_000_000L;
• 09-30-2011, 01:47 AM
mc1234
Re: if statements in JAVA
Okay. Thanks very much. Also The output that the program is giving is not correct. For example for this input..

Years:
24
Months:
3
Days: 19

This should be the output..

Been alive: 766,281,600
Average human life : 2,500,000,000
You have 1,733,718,400 seconds left to live.

However, instead this is the output I keep getting.

You have been alive for:
51667200
Average human life is:
2,500,000,000
You have this much longer to live
2448332800

I've checked over all my calculations numerous times and I can't find the problem.
• 10-01-2011, 12:22 AM
mc1234
Re: if statements in JAVA
This is my calculation portion of the program. But it is not giving out the correct output. Any suggestions? Thanks.

System.out.println ("You have been alive for:");

currentlife = (days * 365 * 24 * 60 * 60);
currentlife = (years * months * days *365 * secondsperday);
System.out.println (currentlife);

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

Long life = (long) 2500000000L;

System.out.println ("You have this much longer to live");
System.out.println (2500000000L - current);
• 10-03-2011, 03:33 AM
bunty83
Re: if statements in JAVA
currentlife = (days * 365 * 24 * 60 * 60);//days in seconds
currentlife = (years * months * days *365 * secondsperday);years and month in seconds
you have to add both currentlife

you are only print out second currentlife line but not counting the days in seconds in total.

try this
currentlife = years * 365 * 24 * 60 * 60);
currentlife = currentlife + (days * 24* 60 *60);
long life = (long) 2500000000l;
lifeleft = life - current life ;
try this