So i have looked around and have seen some similar questions, but i still couldn't get my program to work. I'm just practicing at home (I'm in high school) couldn't leave this question unsolved and move on. This is my code, but i'm not sure what i'm doing wrong.
String inputAge, outputOK, outputCancel;
Integer Age;
inputAge = JOptionPane.showInputDialog("Enter Age To Find Your Year Of Birth", JOptionPane.OK_CANCEL_OPTION);
if (inputAge == JOptionPane.OK_OPTION){
System.out.println("You Were Born In The Year " + (2018 - (Age = Integer.parseInt(inputAge))));
} else if (inputAge == JOptionPane.CANCEL_OPTION){
System.exit(1);
}
that the first type: java.lang.String and the second type: int.
The showInputDialog(...)
method returns a String, not an int. So you can't just assign the value to an int. You need to convert the String to an int. Something like:
String value = JOptionPane.showInputDialog(...);
int age = Integer.parseInt(value);