0

In school I had to make a calculator program. In the program, we ask the user if they want to add, subtract, multiply, or divide. At the end, we ask the user of they want to continue the program or no. I haven't put in the looping part yet, but my problem here is that after the "Would you like to continue" is displayed, the program just exits.

package calculator;

import java.util.Scanner;

public class calculator {

    public static void main(String[] args) {

        {

        int o1; //first operand
        int o2; //second operand

        Scanner input = new Scanner (System.in);
        System.out.println("Enter a choice:");
        System.out.println("+ to add");
        System.out.println("- to subtract");
        System.out.println("* to multiply");
        System.out.println("/ to divide");
        System.out.println("X to exit");
        String userChoice = input.nextLine();

        if (userChoice.equals("X to exit")) {
            System.exit(0);
        }

        System.out.println("Enter the first operand:");
        o1 = input.nextInt();
        System.out.println("Enter the second operand:");
        o2 = input.nextInt();

        if (userChoice.equals("+ to add")) {
            System.out.println( (o1) + (o2) ); }
            else if (userChoice.equals("- to subtract")) {
                System.out.println( (o1) - (o2) ); }
            else if (userChoice.equals("* to multiply")) {
                System.out.println( (o1) * (o2) ); }
            else if (userChoice.equals("/ to divide")) {
                System.out.println( (o1) / (o2) ); }

        System.out.println("Would you like to continue?");
        System.out.println("Yes");
        System.out.println("No");
        String userPick = input.nextLine(); {

        if (userPick.equals("Yes")) {
            System.out.println("Ok."); }
            else if (userPick.equals("No")) {
                System.exit(0); }

        }
        }
        }

        // TODO Auto-generated method stub


}
JJJ
  • 32,902
  • 20
  • 89
  • 102
Evan
  • 870
  • 1
  • 12
  • 24
  • 1
    You need a while(true) loop outside of everything so that it all repeats. – d'alar'cop Sep 07 '13 at 15:13
  • I've had this problem a _lot_ while working with `Scanner`s and `nextLine` after `nextInt`. See e.g. this question. http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx – PurkkaKoodari Sep 07 '13 at 15:14

4 Answers4

0

Try this:

 Scanner input = new Scanner (System.in);
 while(true){
    System.out.println("Enter a choice:");
    System.out.println("+ to add");
    ......
    if (userPick.equals("Yes")) {
        System.out.println("Ok."); }
        else if (userPick.equals("No")) {
            System.exit(0); }

    }
 }

It will continue to loop around the logic until the terminating condition is met. You may also like to close the scanner before System.exit(); and before any termination in fact.

d'alar'cop
  • 2,357
  • 1
  • 14
  • 18
  • How do I add in "ignroecase" in this statement? if (userChoice.equals("+ to add")) { – Evan Sep 07 '13 at 16:02
  • You already ticked a different answer mate. Didn't you need a loop around for it to work? I mean, how does it actually ask again?? – d'alar'cop Sep 07 '13 at 16:05
  • Well if you type in "Yes" then it loops it from the beginning. I did what you said, put in the while(true){. And I unmarked feng smith's answer since yours and his answered my questions. – Evan Sep 07 '13 at 16:34
  • Ok, well for ignorecase... you just set both sides of the comparison to lower/uppercase. E.G "SDASD".toLowerCase().equals("sdasd".toLowerCase()) will return true. C'mon mate, I think I earned a tick :) – d'alar'cop Sep 07 '13 at 16:36
0

You can add a line before your code

String userPick = input.nextLine(); which line is input.nextLine();

it can work well, which can receive enter break line.you can try.

ps:my english is bad,I am not sure I expressed clearly.

Christopher Rucinski
  • 4,737
  • 2
  • 27
  • 58
feng smith
  • 1,487
  • 2
  • 9
  • 22
0
System.out.println("Would you like to continue?");
System.out.println("Yes");
System.out.println("No");
// add this lline, it can make a difference
input.nextLine();
String userPick = input.nextLine();
feng smith
  • 1,487
  • 2
  • 9
  • 22
  • Thanks! This works well. Also, with something like if (userChoice.equals("+ to add")) { How do I put in the "ignorecase"? – Evan Sep 07 '13 at 16:01
  • if (userChoice.equalsIgnoreCase("+ to add")) – feng smith Sep 07 '13 at 16:20
  • Can you expand on how this works to resolve the requester's question? – Ro Yo Mi Sep 07 '13 at 16:39
  • This line "o2 = input.nextInt();",in my opinion, when you inputed your second integer number then pressed enter,in fact it transfer your inputed number and enter break line characters together to the keyboard buffer. the keyboard buffer gives the integer number to o2.when it execute this line:"String userPick = input.nextLine();", it gives the enter break line character to userPick.So userPick.equals("Yes") is false, so it exit. – feng smith Sep 08 '13 at 01:53
0

You need a while(true) {} loop at the place in the program where you want it to restart. This way, you can go back to the beginning if the user says yes, but the program will exit if the user says no.

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120