-9

Could some one please tell me why this calculator isn't working ? It just doesn't provide an awnser.

import java.util.Scanner;

public class Calcu {

   public static void main( String[] args )
   {
       Scanner mati = new Scanner(System.in);

     System.out.println("This program adds up or substracts two numbers");
     System.out.println("Enter an operator");
     String letter = mati.next();         //WAITS FOR THE PHRASE ADD OR SUBSTRACT

     System.out.println("Enter your first number");
     int userNumberone = mati.nextInt();       // Get's first Number

     System.out.println("Enter your second number");
     int userNumbertwo = mati.nextInt();    //Get's Following Number

     if(letter == "add") {
         int result = userNumberone + userNumbertwo;
            System.out.println(result);

     } else if(letter == "substract") {
            int result1 = userNumberone - userNumbertwo;   //If statement to add or substract.
            System.out.println(result1);

     }
   }
}
ruakh
  • 175,680
  • 26
  • 273
  • 307
  • 2
    *soooo* tempting to edit the title to "calculator no worky": http://dilbert.com/strip/2010-03-17 – neminem Feb 06 '15 at 23:40
  • 2
    The title is about the least informative title possible. Also consider trying to explain what you are trying to do and what is happening instead. – helgatheviking Feb 06 '15 at 23:40
  • It's most likely a string comparing issue as @Lashane flagged. You need to compare by value instead of reference. – CalebB Feb 06 '15 at 23:45

1 Answers1

1

You should use letter.equals("add") instead of letters == "add". This is explained here: How do I compare strings in Java?

Community
  • 1
  • 1
Thijs Riezebeek
  • 1,762
  • 1
  • 15
  • 22