Tell me how to repeat the input string, if it can not be converted to type int or double ...

String j = inc.readLine(); // ввели строку // пробуем преобразовать в тип double double resl = Double.parseDouble(j); // если преобразование невозможно, повторить ввод заново ??? 

    1 answer 1

     Double parsing() { String j = inc.readLine(); // ввели строку Double resl; try { resl = Double.parseDouble(j); } catch (Exception e) { return parsing(); } return resl; } 

    The logic is as follows: try ( try ) to parse a value, if it doesn't work out, catch ( catch ) an exception ( Exception e ), and recursively call the same function.

    • Thanks, got it. - Andrey Isenko
    • one
      I would be careful not to use recursion here - if the user is stubborn, you can exhaust the stack :) I would do something like this: while (true) { try { j = inc.readLine(); // ввели строку return(Double.parseDouble(j)); } catch (Exception x) { System.out.print("appropriate message here"); } } while (true) { try { j = inc.readLine(); // ввели строку return(Double.parseDouble(j)); } catch (Exception x) { System.out.print("appropriate message here"); } } while (true) { try { j = inc.readLine(); // ввели строку return(Double.parseDouble(j)); } catch (Exception x) { System.out.print("appropriate message here"); } } - m. vokhm