-
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);
}
}
-
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).
-
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
-
Re: Age Calculator
Thank you very much, I didn't see that before :)