-4

package calculator; import java.util.*;

public class calculator {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Calculator");

    System.out.println("Enter a number");
    double firstNumber = in.nextDouble();

    System.out.println("Enter another number");
    double secondNumber = in.nextDouble();

    System.out.println("Enter the letter for 'm'ultiply 's'ubtract 'a'dd or 'd'ivide");

    String wantedProcess = in.nextLine();       

    String multiply = "m";

    String subtract = "s";

    String add = "a";

    String divide = "d";


    if(wantedProcess.equals(multiply))
    {
        double product = firstNumber * secondNumber;
        System.out.println("=" + product);
    }



}

} so this is the calculator i was making but after i enter two numbers i cant type m s a or d. what am i doing wrong?

Tolga
  • 1
  • 2
  • 2
    possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – BitNinja Sep 14 '14 at 18:09
  • To compare two strings use `equals()` method instead of `==` – XardasLord Sep 14 '14 at 18:10
  • What do you mean you can't type? Does the program exit? In that case do you maybe have a stop condition somewhere in the code that is not posted here? Can we see more of the code? – gkrls Sep 14 '14 at 18:13
  • what i mean is i click on the button that lets me test the code. i type two numbers and then it asks me to enter one of the letters but nothing happens when i try to – Tolga Sep 14 '14 at 18:17
  • 1
    Your question seems to be morphing. Did you have a specific question, or are you just using people to debug your code ... – trooper Sep 14 '14 at 18:29
  • no that was my question all along im sorry i wasnt clear enough – Tolga Sep 14 '14 at 18:31

2 Answers2

0

change this if(wantedProcess.equals(multiply) to if(wantedProcess.equals(multiply))

you missed out )

Edit: Then You need to create a new Scanner object again

   Scanner inF = new Scanner(System.in);
   String wantedProcess = inF.nextLine();
SparkOn
  • 8,806
  • 4
  • 29
  • 34
  • there is one more error im getting. i think the problem is String wantedProcess = in.nextLine(); because i cant type one of the letters when im testing the code – Tolga Sep 14 '14 at 18:26
0
if(wantedProcess.equals(multiply)) 

You missed the second brace at the end. Now look: if you enter "m" for multiply and then compare String "m" with String "Multiply" how do you expect them to be equal? Compare only first letters, for example, using String.charAt() method.

What's more, I guess you'd be better off using Scanner's next() method, rather than nextLine(). Check their docummentation here: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()

3yakuya
  • 2,622
  • 4
  • 25
  • 40