0

When I run my code, it works right up until it asks the question "which operation do you want to use from ( sum , subst , multi , div )". No matter what the user picks, there is no response from my program!

Why is this happening?

import java.util.Scanner;
import java.io.*;

public class three3 {
    public static void main (String[] args) {
        int x;
        int y;
        int opera;
        String oper;

        Scanner in = new Scanner (System.in);
        System.out.println(" write the first number ");
        x = in.nextInt();

        System.out.println(" write the second number ");
        y = in.nextInt();

        System.out.println(" which operation do you want to use from ( sum , subst , multi , div )");
        oper = in.nextLine();

        if (oper == "sum") {
            opera=x+y;
            System.out.println(" the sum of two numbers is " + opera );
        }

        if (oper == "subst") {
            opera = x - y;
            System.out.println(" the subtraction of two numbers is " + opera );
        }

        if (oper == "multi") {
            opera = x * y;
            System.out.println(" the multi of two numbers is " + opera );
        }

        if (oper == "div") {
            opera = x / y;
            System.out.println(" the division of two numbers is " + opera );
        }
    }
}
Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
ooooo
  • 53
  • 6

4 Answers4

3

Because none of those if-clauses is executed. You're comparing Strings with == which is wrong. Use oper.equals("sum") instead. See this question for reference. The conclusion for you is to always use equals for Strings.

Community
  • 1
  • 1
runDOSrun
  • 10,359
  • 7
  • 47
  • 57
2

You need to call in.nextLine() right after the last call to in.nextInt() The reason is that just asking for the next integer doesn't consume the entire line from the input, and so you need skip ahead to the next new-line character in the input by calling in.nextLine().

int y = in.nextInt();
in.nextLine();

This pretty much has to be done each time you need to get a new line after calling a method that doesn't consume the entire line, such as when you call nextBoolean() etc.

In addition, you don't check for String equality with the == operator, use the .equals() String method instead.

SamTebbs33
  • 5,507
  • 3
  • 22
  • 44
1

The problem is that in.nextLine() consumes the \n inserted implicitly when you clicked enter after the int was entered. That means that the program doesn't expect any other input from the user. To fix this you could consume a new line with in.nextLine() before putting it int your actual variable, something like this:

System.out.println(" write the second number ");
y=in.nextInt();

System.out.println(" which operation do you want to use from ( sum , subst , multi , div )");

in.nextLine(); //New line consuming the \n

oper=in.nextLine();

if(oper.equals("sum")){//replace == by .equals
   opera=x+y;
}

Apart from that, and as runDOSrun said, you should replace the comparisons of strings from a==b to a.equals(b)

Juan
  • 1,754
  • 1
  • 14
  • 22
0

Adding on to other people's points, you should also consider using else if{} and else{} statements so you can catch invalid input.

deezy
  • 1,480
  • 1
  • 11
  • 22