• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to calculate total age in months and store is as variable?  RSS feed

 
amir Ghannad
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I am very new to java. I was wondering how can I calculate total age in months and store it as variable? this is what I have so far:



*
* You will ask the user to provide their name (as a single word, assume first name here)
* You will ask the user their age in full years (no fraction or months)
* You will ask the user how many full months since their last birthday (again, whole numbers here).
*
* You will use this information to calculate the user's age in months and use that information to print
* out a greeting.
*/







 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, amir.

What you have so far looks correct, as far as calculating the months, except that you should add the months since the last birthday.

There are a few things about your code not related to the question you have.

  • You probibly don't want s.nextLine(). Try s.next() instead.
  • Normally you should close an open resource; System.in is the one exception. Remove s.close()
  •  
    amir Ghannad
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank for your tip just fixed those. Now, I am confused here what did I do wrong? how to show" any other age here?" here is my code:
    /*



     
    Liutauras Vilda
    Sheriff
    Posts: 4917
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    amir gh wrote:Thank for your tip just fixed those. Now, I am confused here what did I do wrong? how to show" any other age here?" here is my code:
    /*



    Hi amir,

    Never ever do not omit curly braces in your code around if, else, while, for. Beginners very like to cut corners and produce code as quick as possible. Unfortunately more often it fails than succeeds.
     
    Knute Snortum
    Sheriff
    Posts: 4276
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You don't need the final if(), just else will do. If you need a refresher on if-then-else statements, click here.
     
    Liutauras Vilda
    Sheriff
    Posts: 4917
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Your code is understood like this now. Think what is wrong?
     
    amir Ghannad
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    that is exactly where I confused . You are right but I don't know how I am supposed to fix this.
    How about this:
     
    Liutauras Vilda
    Sheriff
    Posts: 4917
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Liutauras Vilda wrote:Never ever do not omit curly brackets in your code around if, else, while, for.


    Start from most important things.
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12563
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    amir gh wrote: How about this:

    Look VERY CAREFULLY at this. If this really is your code, it will print that line every single time - due to one character.

    You may want to review the proper syntax of an if-statement.
     
    Liutauras Vilda
    Sheriff
    Posts: 4917
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I found for you very easy understandable website, have a look > http://www.homeandlearn.co.uk/java/java_if_else_statements.html
     
    amir Ghannad
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Nice. Thanks that was helpful.
     
    Liutauras Vilda
    Sheriff
    Posts: 4917
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    amir Ghannad wrote:Nice. Thanks that was helpful.


    So, please post it here what you got, let us enjoy
     
    amir Ghannad
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here


     
    Liutauras Vilda
    Sheriff
    Posts: 4917
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Better, but not good enough.

    Luckily Ranch got some useful tips for you about code indentation > http://www.javaranch.com/styleLong.jsp#indent

    Take it very seriously, as it can become a very bad habit, which could be difficult to get rid off.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!