NumberFormatException not wrking java

You should read new inputs inside your loop:

try {
    while (true) { 

        num = Integer.parseInt(data);

catch  (NumberFormatException e)  {


Currently you are adding the same input infinite times to your List.

P.S. perhaps you shouldn’t use an infinite while loop. How do you plan to finish reading the inputs? By catching a NumberFormatException when the user enters an invalid number? It’s not a good practice to use exceptions as part of your logic.

Leave a Comment