Results 1 to 16 of 16
Like Tree2Likes
  • 2 Post By Iron Lion

Thread: if statements in JAVA

  1. #1
    mc1234 is offline Member
    Join Date
    Sep 2011
    Posts
    24
    Rep Power
    0

    Default 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. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,875
    Rep Power
    8

    Default 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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    mc1234 is offline Member
    Join Date
    Sep 2011
    Posts
    24
    Rep Power
    0

    Default 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. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: if statements in JAVA

    Quote Originally Posted by mc1234 View Post
    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. #5
    mc1234 is offline Member
    Join Date
    Sep 2011
    Posts
    24
    Rep Power
    0

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

  6. #6
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default 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. #7
    mc1234 is offline Member
    Join Date
    Sep 2011
    Posts
    24
    Rep Power
    0

    Default 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. #8
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default 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();
    Fubarable and Solarsonic like this.

  9. #9
    mc1234 is offline Member
    Join Date
    Sep 2011
    Posts
    24
    Rep Power
    0

    Default Re: if statements in JAVA

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

  10. #10
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: if statements in JAVA

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

  11. #11
    mc1234 is offline Member
    Join Date
    Sep 2011
    Posts
    24
    Rep Power
    0

    Default 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. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: if statements in JAVA

    Long != long

    just like
    Double != double.

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

  13. #13
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default 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. #14
    mc1234 is offline Member
    Join Date
    Sep 2011
    Posts
    24
    Rep Power
    0

    Default 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. #15
    mc1234 is offline Member
    Join Date
    Sep 2011
    Posts
    24
    Rep Power
    0

    Default 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. #16
    bunty83 is offline Member
    Join Date
    Oct 2011
    Location
    North Carolina
    Posts
    5
    Rep Power
    0

    Default 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

Similar Threads

  1. how to call multiple sql statements in java
    By sandeep43 in forum JDBC
    Replies: 5
    Last Post: 08-01-2011, 10:13 AM
  2. How to call set of sql statements in java?
    By sandeep43 in forum JDBC
    Replies: 5
    Last Post: 05-31-2011, 06:58 AM
  3. How to call set of sql statements in java?
    By sandeep43 in forum New To Java
    Replies: 0
    Last Post: 05-30-2011, 06:37 AM
  4. Replies: 9
    Last Post: 02-14-2011, 04:22 AM
  5. Help with if else statements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 07:56 PM

Posting Permissions

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