Java: stop a Constructor when System.in reads in a specific String

37 Views Asked by At

I have some trouble with a Constructor in Java. The Problem is that I have to read-in some input from the Systme.in in the Constructor to initialize some final variable. Now i have to quit the Program when the input is "quit". For this, it's not possible to use Systme.exit() or Runtime.exit(). I have two Ideas for this Problem, but both have some Problems.

Solution 1: One Idea would be to set a variable to true when the input is "quit" and check the variable after each Question, but that wouldn't be a nice Code.

public Game() {
        input = new Scanner(System.in);
        this.gameFinished = false;
        this.moveNumber = 0;
        printLogo();

        //Get Starting Information TODO Magic Numbers

        
        this.playerCount = getIntegerWithCondition(1, "Question 1?");
        //check if gameFinished = true
        getPlayerNames();
        //check if gameFinished = true
        this.startGold = getIntegerWithCondition(0, "Question 2?");
        //check if gameFinished = true
        this.winGold = getIntegerWithCondition(1, "Question 3?");
        //check if gameFinished = true
        this.shuffleSeed = getIntegerWithCondition(0, "Question 4?");
        //check if gameFinished = true
    }

private int getIntegerWithCondition(int condition, String question) {
        System.out.println(question);
        boolean inputValid = false;
        int playerValue = 0;
        while (!inputValid) {
            String playerInput = input.nextLine();
            if (playerInput.equals("quit")) {
                this.gameFinished = true;
                break;
            }
            if (playerInput.matches("-*[0-9]+")) {
                try {
                    playerValue = Integer.parseInt(playerInput);
                    if (playerValue > condition) {
                        inputValid = true;
                    } else {
                        System.out.println(String.format("Error: Your Input must be greater than %d!", condition));
                    }
                } catch (NumberFormatException e) {
                    //TODO
                }
            } else {
                System.out.println("Error: Your Input is not a Number!");
            }
        }
        return playerValue;
    }

    private void getPlayerNames() {
        for (int i = 1; i <= this.playerCount; i++) {
            System.out.println(String.format("Enter the name of player %d:", i));
            String userInput = null;
            Boolean inputValid = false;
            while (!inputValid) {
                userInput = input.nextLine();
                if (userInput.equals("quit")) {
                    this.gameFinished = true;
                    break;
                }
                if (userInput.matches("[A-Za-z]+")) {
                    inputValid = true;
                } else {
                    System.out.println("Error: Your input can only contain characters from a to z!");
                }
            }
            players.add(new Player(userInput));
        }
    }

Solution 2: The other Idea I had would be to throw an exception in those two methods and catch them in the constructor. For this I would have to surround those Questions with a try and catch block, but then I have the Problem that the final variables might not be initialized and because they are final I can't initialize them before. Does anybody have an Idea how to solve this problem with a clean code?

0

There are 0 best solutions below