Your code has two major problems and one minor.
Semicolon in if
First one is the semicolon right after the if-statement. The semicolon finishes the if-statement, it is short for the if-statement without curly braces, like:
if (a > b) doMethod();
By omitting an expression and only writing the semicolon you represent a valid NOP (no operation), so
if (a > b) ;
is valid statement which basically does nothing.
You probably intended
if (number == 2) {
System.out.println("Correct.");
} else {
System.out.println("Incorrect. The answer is 2.");
}
Comparison
The other problem is that your number
variable is a String
but you compare it with an int
. That won't work, the result will always be false
. You will need to convert either the String
to int
or vice versa for the comparison to work.
Note that comparing String
with String
using ==
does not work as you might expect (see How do I compare strings in Java?) for details, use String#equals
instead.
So one possibility would be String
with String
:
String number = user_input.next();
if (number.equals("2")) {
The other is int
with int
:
String number = user_input.next();
int asValue = Integer.parseInt(number);
if (asValue == 2) {
or directly use Scanner#nextInt
:
int number = user_input.nextInt();
if (number == 2) {
Missing semicolon
The minor problem is that you forgot a semicolon after the following statement
System.out.println("Solve for x.") // Semicolon needed
In Java every expression must end with a semicolon, it is a strict language.