Stack Exchange
Abort with a string "abort" a while-loop in this simple calculator program made with scanner class and Java
Please help me to abort the while loop with sc.equals("abort") input/n at any time in the process.
  public class Calculator {
        public static void main(String[] args) {
            double wert1, wert2, ergebnis;
            Scanner sc = new Scanner(System.in);
            boolean start = true;
            while(start) {
                try {
                    System.out.println("Pleaser Enter First Number");
                    wert1 = sc.nextDouble();

                    System.out.println("please Enter a Operator +,-,/,*");
                    String operator = sc.next();

                    System.out.println("please Enter second Value");
                    wert2 = sc.nextDouble();

                    if (operator.equals("+")) {
                        ergebnis = wert1 + wert2;
                        System.out.println("Das Ergebnis ist " + ergebnis);
                    }
                    if (operator.equals("-")) {
                        ergebnis = wert1 - wert2;
                        System.out.println("Das Ergebnis ist " + ergebnis);
                    }
                    if (operator.equals("*")) {
                        ergebnis = wert1 * wert2;
                        System.out.println("Das Ergebnis ist " + ergebnis);
                    }
                    if (operator.equals("/")) {
                        ergebnis = wert1 / wert2;
                        System.out.println("Das Ergebnis ist " + ergebnis);
                    }
                    if (sc.equals("abort")) {//why doesnt this work?
                        start = false;
                        break;
                    }
                } catch (InputMismatchException e) {

                    if (sc.equals("abort")) {//why doesnt this work?
                        start = false;
                        break;
                    }
                    else{sc.nextLine();}
                }
            }
        }
    }

Abort with a string "abort" a while-loop in this simple calculator program made with scanner class and Java.

sc is the Scanner object, it does not represent directly what you type. You can retrieve what you type with sc.next().
You already defined String operator = sc.next(), so just use if (operator.equals("abort")) instead of if (sc.equals("abort")).

Yes, thanks. But I would like abort the operation immediatly
when i just add if (sc.equals("abort"))
I have to fill out first the other queries first and only then the process then is finished

Sorry but I'm not sure I understand correctly. If you change your condition like I said, and change you catch statement to simply start = false;, you will stop your while loop any time you type " abort ". Isn't that what you want to do ?

Sorry tryin to do this doesnt work
added  if (operator.equals("abort"))  in the try block, and sc.nextLine();
start = false; break; in the exception tried a lot other combinations. I want to have a loop going calculating infinite only abort when the word abort is pressed doesnt work sorry

Your catch should be : catch (InputMismatchException e) { start = false;       }.
After the line String operator = sc.next();, add this :
if (operator.equals("abort")) {break;}.
Is it working now ?

Yes thanks a lot it is a little bit better,
now it can abort at every step. But at the first step i have to put in a double number else i get thrown out. At the next step i have to enter a operator in String and I can go only out with abort and thats fine, but also in the next step. I have to insert another double number anything else will end the process. I thought catch some other exceptions but at the double input step it wouldnt help i think because it only recognizes the mismatch exception at this step and not the string word abort maybe. i dont know, sorry. Thank you for your help
