In general, the essence of the question is that there is a method that takes coordinates from the console, if the coordinates do not validate throws an exception and go out of work, but it is necessary that while the method works until it works

the code itself looks like this

case 3: System.out.println("Ход белого игрока"); System.out.println("Введите координату в формате e2-a4"); Scanner sc2=new Scanner(System.in); String sss2=sc2.nextLine(); while( menu.executeMenu(White,sss2)){ menu.executeMenu(White,sc2.nextLine()); } System.out.println("Ход черного игрока"); System.out.println("Введите координату в формате e2-a4"); String sss3=sc2.nextLine(); menu.executeMenu(Black,sss3); break; 

This is a game of chess, in general one player makes a move, then the second and everything would be fine, but if the coordinates are not correct, then instead of requesting them, the program moves to the second player’s move

Can you tell me the concept of how to implement it?

1. The step takes the coordinates 2. We transfer them to the method - if the method has worked we pass them below if no, we return to point 2 until the method has done the method itself has returned a boolean value

 public boolean executeMenu(Gamer gamer, String s) { try { String from = s.split("-", 2)[0].toLowerCase(); String to = s.split("-", 2)[1].toLowerCase(); Postition a = new Postition(Interpritator.reverseInter(from).get(0), Interpritator.reverseInter(from).get(1)); Postition b = new Postition(Interpritator.reverseInter(to).get(0), Interpritator.reverseInter(to).get(1)); if (Game.validEat(a, b) && gamer.checkGamerColor(Field.getFigure(a.getX(), a.getY()))) { gamer.eatFigure(a, b); print(); return false; } else if (Game.validMove(a, b) && gamer.checkGamerColor(Field.getFigure(a.getX(), a.getY()))) { gamer.moveFigure(a, b); print(); return false; } else System.err.println("Ход не доступен"); // else System.err.println("Эта фигура не вашего цвета"); } catch (Exception e) { //e.printStackTrace(); System.err.println("Вводи правильные координаты "); } return false; } 
  • one
    You need a method that validates the coordinates. We write coordinates to him, he checks their validity. If the move is valid - perform the move on the board and transfer the move to another player, if not - repeat the request again - Serhii Dikobrazko
  • so what's the difference what to cycle? This method also checks for me the validity and works it out right away. The whole problem is how to make the program execute the method until it stops throwing exceptions. - Alekseev Stanislav
  • 2
    @ Alekseevstanslav when you need to loop something, obviously you need to use a cycle. - Sergey Gornostaev

2 answers 2

Try this:

 Scanner sc = new Scanner(System.in); while (true){ try { String inputLine = sc.nextLine(); // То, что может вызвать ошибку break; } catch (Exception e) { /* Можно предупредить, что некорректные входные данные */ } } 

In an infinite loop, we perform an operation in a try-catch block, and if try has completed successfully, then the loop will be stopped.

It is possible so:

 void thatMethod() { try { String inputLine = sc.nextLine(); // То, что может вызвать ошибку } catch (Exception e) { /* Можно предупредить, что некорректные входные данные */ thatMethod(); } } 
  • neither the first nor the second option does not work - Stanislav Alekseev
  • The 1st option makes a circle once and the program just ends - Stanislav Alekseev
  • the second option is not applicable, as this method takes arguments and needs to be obtained before passing the option also does not suit - Stanislav Alekseev
  • Roughly speaking, I need to focus on the performance of the method — if the method throws an exception — repeat not throwing out of the cycle - Stanislav Alekseev
  • In the latter version, the persistent player can bring the game to stackoverlow :) - Sergey

In general, such a solution to this issue here in this design

in such a construction, the method will be executed until it is executed without error — take it into service

 bolean b; do { try { b=false } catch { b=true } } while(true)