Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Age Calculator

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Age Calculator

    This is my first on-my-own java project I have been working on using the Eclipse program, I have an issue, the eclipse is having an Error with my code somewhere, I can't figure out why.. maybe it's just because i'm a noob? idk, but anyhow, I am trying to make an age calculator only using the year you were born and the current year, (2012), so here's the code.. please help me figure out what's wrong and what I can do to fix it, thank you for your help!

    import java.util.Scanner;
    public class Detector {

    public static void main(String[] args){


    int number, answer, i = 2012;
    Scanner age = new Scanner(System.in);

    System.out.println("Enter your year of birth:");

    number = age.nextInt();
    answer = i - age;

    System.out.println("You are/will be this age: " +answer);

    }

    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,152
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: Age Calculator

    When posting code, please use the highlight tags.

    What is the error? Is it a compile time error (if so copy and paste it here) or a run time error (if so copy and paste the stack trace here).
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Age Calculator

    Your problem is when you are trying to give a value to your answer variable. You are subtracting age, which is a Scanner object and cannot be subtracted from an integer value. What you want to do is subtract number from i

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Age Calculator

    Thank you very much, I didn't see that before

Similar Threads

  1. Calculator Using AWT
    By Allicat in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 17th, 2011, 06:49 AM
  2. Calculator
    By Andrew Wilson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 07:08 AM
  3. Calculator
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2010, 08:00 AM
  4. [SOLVED] Calculator help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 04:27 PM
  5. Calculator help.
    By Skinnyskinny in forum Java Theory & Questions
    Replies: 6
    Last Post: August 1st, 2009, 12:34 PM

Tags for this Thread