# Thread: if statements in JAVA

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

## if statements in JAVA

I need help!! I don't get how if statements would work in this program? Can someone please explain?
Last edited by mc1234; 10-01-2011 at 12:16 AM.

2. ## 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.

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

## 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.

4. ## Re: if statements in JAVA

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.

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

## Re: if statements in JAVA

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.

6. Senior Member
Join Date
Nov 2010
Posts
210
Rep Power
7

## 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.

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

## 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
Last edited by mc1234; 10-01-2011 at 12:17 AM.

8. Senior Member
Join Date
Nov 2010
Posts
210
Rep Power
7

## 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:
Java Code:
`int years = kbd.nextInt();`

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

## Re: if statements in JAVA

But wont the added up seconds be more that the int variable can hold?

10. Senior Member
Join Date
Nov 2010
Posts
210
Rep Power
7

## Re: if statements in JAVA

Possibly. You can use nextLong() if you prefer, or cast it to long later.

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

## 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!

12. ## Re: if statements in JAVA

Long != long

just like
Double != double.

Also, you can't have commas in an int or long literal.

13. Senior Member
Join Date
Nov 2010
Posts
210
Rep Power
7

## 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:
Java Code:
`long humanLife = 2_500_000_000L;`
Last edited by Iron Lion; 09-29-2011 at 08:38 PM.

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

## 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.

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

## 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);

16. Member
Join Date
Oct 2011
Location
North Carolina
Posts
5
Rep Power
0

## 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

#### Posting Permissions

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