0

I'm making a program where it asks the user a simple math question and it has to tell them whether they are correct or incorrect. I'm getting errors

package exercises;

import java.util.Scanner;
public class Exercises {


    public static void main(String[] args) {
        Scanner user_input = new Scanner (System.in);
        String number;
        System.out.println("Consider the following equation: 5x - 10");
        System.out.println("Solve for x.")
        number = user_input.next();

        if (number == 2);{
            System.out.println("Correct.");

        }else{
            System.out.println("Incorrect. The answer is 2.");
        }

}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
ernest
  • 49
  • 8
  • 6
    `if (number == 2);{` literally everything about this statement is wrong. `number` is a String, should not be compared using `==` and cannot be compared to an `int` - or `number` should be an `int` to begin with. And drop the `;`. – luk2302 Feb 10 '18 at 17:28
  • 1
    Need to drop the semi colon, but error is answered in https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – OneCricketeer Feb 10 '18 at 17:31
  • 1
    Please always include the error message instead of just saying "it doesn't work". What did you expect and what did actually happen? – Zabuzard Feb 10 '18 at 17:33

3 Answers3

0

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.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
0

You have two syntax errors and both are related to semicolon ;.

The first error is this line System.out.println("Solve for x."). In Java every statement must end with ;. This line has to be System.out.println("Solve for x.");.

The second error is in if statement: if (number == 2);{. You should remove semicolon after close parentheses. The correct line of code is if (number == 2) {.

djm.im
  • 3,295
  • 4
  • 30
  • 45
-3

You are trying to compare number which is a String, with an integer and it can't be done, here you have two option

1) read an integer from System.in

public static void main(String[] args) {
        Scanner user_input = new Scanner(System.in);
        String number;
        System.out.println("Consider the following equation: 5x - 10");
        System.out.println("Solve for x.");
        number = user_input.next();

        if (Integer.parseInt(number) == 2) {
            System.out.println("Correct.");

        } else {
            System.out.println("Incorrect. The answer is 2.");
        }
    }

2) parse the number as integer before comparing it with the int

public static void main(String[] args) {
        Scanner user_input = new Scanner(System.in);
        int number;
        System.out.println("Consider the following equation: 5x - 10");
        System.out.println("Solve for x.");
        number = user_input.nextInt();

        if (number == 2) {
            System.out.println("Correct.");

        } else {
            System.out.println("Incorrect. The answer is 2.");
        }
    }
Arpan Sarkar
  • 2,301
  • 2
  • 13
  • 24