0
import java.util.Scanner;


public class Calculator {
static double num1 = 0, num2 = 0;
static Scanner input = new Scanner(System.in);
static int choice = 0;
static double answer = 0;



public static void main(String args []){
    enter(num1, num2);
}

public static void enter(double num1, double num2) {
    System.out.println("********CALCULATOR*********");
    System.out.println("Type in your first number.");
    num1 = input.nextDouble();
    System.out.println("Type in your second number.");
    num2 = input.nextDouble();
    System.out.println("Enter the number associated with what you would like to do.");
    System.out.println("1. Add them");
    System.out.println("2. Subtract them");
    System.out.println("3. Multiply them");
    System.out.println("4. Divide them");
    choice = input.nextInt();
    operations(num1, num2, choice);

}

public static void operations(double num1, double num2, int choice) {
    if(choice == 1){
        answer = num1 + num2;
    }else if(choice == 2){
        answer = num1 - num2;
    }else if(choice == 3){
        answer = num1 * num2;
    }else if(choice == 4){
        answer = num1 / num2;
    }
    printAnswer(answer);
}

public static void printAnswer(double answer) {
    boolean re;
    String tryAgain;
    System.out.println("The answer is " + answer);
    System.out.println("Would you like to use it again? Yes or No.");
    tryAgain = input.next();
    if(tryAgain == "yes"){
        re = true;
        restart(re);
    }else if (tryAgain == "no"){
        re = false;
        restart(re);
    }

}

public static void restart(boolean re) {
    if(re == true){
        enter(num1, num2);
    }
    if (re == false){
        System.exit(0);
    }

}
}

I'm trying to make a calculator, but I ran into a little problem... The problem is occurring after

tryAgain = input.next();

I don't think the rest of my code is being run The Scanner works properly but the program quits after I type something in.

Ved115
  • 23
  • 1

3 Answers3

1

You can't use == to compare strings in java. Use:

tryAgain = input.next();
if(tryAgain.equals("yes")){
    re = true;
    restart(re);

etc...

you can also use .equalsIgnoreCase if you don't care about capitalization

Android Newbie
  • 711
  • 3
  • 9
0

Use equals() method instead of ==

  if(tryAgain.equals("yes")){
        restart(true);
    }else if (tryAgain.equals("no")){
        restart(false);
    }
Kick
  • 4,823
  • 3
  • 22
  • 29
0
package test;

import java.util.Scanner;

public class Calculator {
    static double num1 = 0, num2 = 0;
    static Scanner input = new Scanner(System.in);
    static int choice = 0;
    static double answer = 0;

    public static void main(String args[]) {
        enter(num1, num2);
    }

    public static void enter(double num1, double num2) {
        System.out.println("********CALCULATOR*********");
        System.out.println("Type in your first number.\n");
        num1 = input.nextDouble();
        System.out.println("Type in your second number.\n");
        num2 = input.nextDouble();
        System.out
                .println("Enter the number associated with what you would like to do.");
        System.out.println("1. Add them");
        System.out.println("2. Subtract them");
        System.out.println("3. Multiply them");
        System.out.println("4. Divide them\n");
        choice = input.nextInt();
        operations(num1, num2, choice);

    }

    public static void operations(double num1, double num2, int choice) {
        if (choice == 1) {
            answer = num1 + num2;
        } else if (choice == 2) {
            answer = num1 - num2;
        } else if (choice == 3) {
            answer = num1 * num2;
        } else if (choice == 4) {
            answer = num1 / num2;
        }
        printAnswer(answer);
    }

    public static void printAnswer(double answer) {
        boolean re;
        String tryAgain;
        System.out.println("The answer is " + answer);
        System.out.println("Would you like to use it again? Yes or No.");
        tryAgain = input.next();
        if (tryAgain.equalsIgnoreCase("yes")) { // don't compare Strings using ==
            re = true;
            restart(re);
        } else if (tryAgain.equalsIgnoreCase("no")) {
            re = false;
            restart(re);
        }

    }

    public static void restart(boolean re) {
        if (re == true) {
            enter(num1, num2);
        }
        if (re == false) {
            System.exit(0);
        }

    }
}

The only change I've made is in line tryAgain == "yes" ! Use equals() or equalsIgnoreCase() to compare strings !

Look at the exapmle:

String s1 = "text1";
String s2 = "text1";
System.out.println(s1 == s2); // true 
System.out.println(s1.equals(s2)); // true

String s3 = new String("text1");
String s4 = new String("text1");
System.out.println(s3 == s4); // false because you are comparing references but not the objects
System.out.println(s3.equals(s4)); // true , because you are comapring objects
ikos23
  • 4,879
  • 10
  • 41
  • 60