Why does this loop forever

Question:
I cant understand why this is happening
Repl link:
Ephraim mart test - Replit

      int whichFood = 0;
      int howMany = 0;

      double basePrice = 0.0;

      out.println("Which one do you want?");
      out.println("(Please don't enter a typed out number like \"one\", enter a numeric one like\"1\"");
      for (int i = 0; i < martFruit.length; i++) {
        out.println((i + 1) + ") " + martFruit[i].get("name") + " - " + martFruit[i].get("price"));
        // prints list of fruit and their prices
      }
      while (whichFood == 0) {
        whichFood = 0;
        try {
          whichFood = input.nextInt();
          out.println("OK!");
          out.println("How many do you want? (limit - 100)");
          out.println("(Please don't enter a typed out number like \"one\", enter a numeric one like\"1\"");
          while (howMany == 0) {
            howMany = 0;
            try {
              howMany = input.nextInt();
              if (howMany > 0 && howMany < 101) {
                out.println("OK! " + howMany + " it is!");
              } else {
                out.println("Really?");
                out.println("Try again.");
                out.println("A");
                howMany = 0;
              }
            // if else works
            } catch (Exception e) {
              out.println("Really?");
              out.println("Try again.");
              out.println("B");
              howMany = 0;
            }
          }
        } catch (Exception e) {
          out.println("Really?");
          out.println("Try again.");
          out.println("C");
          whichFood = 0;
        }

      }

(the varible martFruit is an arraylist and is not causing the error)

any help would be great!

the lettersin the else statements and the catch statements are so I knov which one is the code repeating

Hi @BananaPi

Thanks for your message.

Can you describe what steps you go through in your program before it loops forever?

OK, once you get to the fruit code, enter a letter and it will repeat: Really? Try again,

Ah yes, I see that now. I entered a number like it was asking before.

Yeah, I am trying to make the code fool-proof

I’ve been playing around with the code and managed to get it to loop infinitely for B and then A. I think the issue is that you are using the same Scanner object (input) for each entry, so if there is an exception it continues when the while loop runs again.

One suggestion would be to use a different Scanner object for each input, then you shouldn’t get the infinite loop of exceptions. Hope this helps!

I have tried using another scanner and it doesn’t work

Ok. I figured it out. I nedded to use

input.next();

to clear the input varible

Hello @BananaPi
To end the loop(which is infinite loop of forever loop) use this statement.

break;

Example of this

class Main {

    public static void main(String[] args) {
        for (int n = 1; n <= 5; n++) {
            System.out.println("*");
            if (n == 2) {
                break;
            }
        }
    }
}

In example I have for loop

Hope it works,
If it works Mark it as Solution

firstly, that loop is not a while loop that doesn’t terminate (an infinite loop). Also, if you read the rest of the thread, I believe what’s happening is that there is something about nonterminating input statements