简体   繁体   中英

Compare user input with integer

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.");
        }

}

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.

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) { .

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.");
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM