Reputation: 21
I have a project in my programming class where I have to find the age of someone by taking the current year and subtracting their birth year. However, when I wrote the code, it keeps giving me the answer of "50.0minus50.0=0.0".
import java.util.Scanner;
public class ageMath {
String firstName;
static int currentYear;
static int birthYear;
double mathEquation;'
Scanner lookAtKeyboard = new Scanner(System.in);
public void getFirstName() {
System.out.println("What is your first name");
firstName = lookAtKeyboard.nextLine();
}
public void getCurrentYear() {
System.out.println("What is the current year");
currentYear = lookAtKeyboard.nextLine().charAt(0);
}
public void getBirthYear() {
System.out.println("What is your birth year");
birthYear = lookAtKeyboard.nextLine().charAt(0);
}
public void doMath(double birthYear, double currentYear) {
mathEquation = currentYear - birthYear;
System.out.println(currentYear +"minus"+ birthYear + "=" + mathEquation);
}
public static void main (String[] args) {
ageMath calculator = new ageMath();
calculator.getFirstName();
calculator.getCurrentYear();
calculator.getBirthYear();
calculator.doMath(currentYear,birthYear);
}
}
What should I do in order to fix this?
Upvotes: 1
Views: 357
Reputation: 4465
char(0)
returns the ascii value of '2' as you have probably entered 2019
as current year.
See ASCII tabel
If you want the same input behaviour (user presses enter after each input) you can read the input as string and parse it to an int:
public void getCurrentYear() {
System.out.println("What is the current year");
currentYear = Integer.parseInt(lookAtKeyboard.nextLine());
}
public void getBirthYear() {
System.out.println("What is your birth year");
birthYear = Integer.parseInt(lookAtKeyboard.nextLine());
}
(please also look at my comment regarding the order of parameters when calling doMath in the main method.
Upvotes: 1
Reputation: 1
For int you have to use nextInt()
.The java string charAt()
method returns a char value
at the given index number.Which is not suitable for the current scenario of yours.
import java.util.Scanner;
class Main {
String firstName;
static int currentYear;
static int birthYear;
double mathEquation;
Scanner lookAtKeyboard = new Scanner(System.in);
public void getFirstName()
{
System.out.println("What is your first name");
firstName =lookAtKeyboard.nextLine();
}
public void getCurrentYear() {
System.out.println("What is the current year");
currentYear =lookAtKeyboard.nextInt();
}
public void getBirthYear() {
System.out.println("What is your birth year");
birthYear =lookAtKeyboard.nextInt();
}
public void doMath(double birthYear, double currentYear) {
mathEquation = currentYear - birthYear;
System.out.println(currentYear +"minus"+ birthYear + "=" +mathEquation);
}
public static void main (String[] args) {
Main calculator = new Main();
calculator.getFirstName();
calculator.getCurrentYear();
calculator.getBirthYear();
calculator.doMath( currentYear,birthYear);
}
}
Working link (I just changed the class name ,so that it can work on fiddle)
Upvotes: 3