1

I am having trouble getting this program to work properly, it takes 2 numbers then does the type of math you tell it to do. I wrote this just to refresh my memory having not worked with java for a few months. Here is the code:

import java.util.Scanner;
public class math {
    public static void main(String[] args){
        Scanner one = new Scanner(System.in);
        Scanner two = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n1 = one.nextInt();
        System.out.print("Enter a second number: ");
        int n2 = one.nextInt();
        System.out.print("Add, subtract, multiply, or divide? ");
        String f = two.nextLine();
        System.out.println(f);
        if (f=="add"){
            System.out.println(n1 + n2);
        }else if (f=="subtract") {
            if (n1>n2){
                System.out.println(n1 - n2);
            }else{
                System.out.println(n2 - n1);
            }
        }else if (f=="multiply"){
            System.out.println(n1 * n2);
        }else if (f=="divide"){
            if (n1>n2){
                System.out.println(n1 / n2);
            }else{
                System.out.println(n2 / n1);
            }
        }
    }
}

Edit: Thank you all, it works with f.equals()

2
  • 2
    String.equals(String) Commented Sep 15, 2012 at 16:00
  • String.equals() compares content. == compares locations. For comparing string literals and interned strings, == will work. In all other cases, .equals() is the only option. Basically, .equals() Commented Sep 15, 2012 at 16:03

4 Answers 4

3

Your String comparisons need to be changed to use String.equals() instead of ==:

if (f=="add"){

To

if ("add".equals(f)){

and

    }else if (f=="subtract") {

to

    }else if ("subtract".equals(f)) {

etc.

Sign up to request clarification or add additional context in comments.

2 Comments

That worked, thank you. Is there a similar way to test if there is a single character or certain string of text inside of variable f? Such as this: if (f contains the string "enter string here") {do this}
f.contains(CharSequence c) :) you can use a string in the place of a charsequence
3
if (f=="add")

compares references. You need to

if(f.equals("add"))

because java has many objects and mainly == is used for reference comparing unless you are not doing it for primitives.

If you want to check if it has a string inside,

if(f.contains("asdas"))

Comments

3

You're comparing strings with ==, use f.equals("add") instead (same with other comparisons). Generally, == should only be used on primitive data types as opposed to objects (like Strings).

import java.util.Scanner;
public class math {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n1 = in.nextInt();
        System.out.print("Enter a second number: ");
        int n2 = in.nextInt();
        System.out.print("Add, subtract, multiply, or divide? ");
        String f = in.nextLine();
        System.out.println(f);
        if (f.equals("add")) {
            System.out.println(n1 + n2);
        } else if (f.equals("subtract")) {
            if (n1 > n2){
                System.out.println(n1 - n2);
            } else {
                System.out.println(n2 - n1);
            }
        } else if (f.equals("multiply")) {
            System.out.println(n1 * n2);
        } else if (f.equals("divide")) {
            if (n1>n2) {
                System.out.println(n1 / n2);
            } else {
                System.out.println(n2 / n1);
            }
        }
    }
}

Also, there's no need to use two Scanner instances, one will suffice. Just as a side note, strings have an equalsIgnoreCase method as well which will test for equality but ignore the cases of the characters.

Comments

0

While

f.equals("add");

will work, if I enter Add, it won't work. To fix this you can replace f.equals("word") with

f.equalsIgnoreCase("add");

which allows any case, AdD will work, ADD will work, aDD will work, etc.

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.