mercredi 16 juin 2021

Why is my validation function not working (to validate if user input is an integer within range)

I'm trying to make a validation function, but as I tried to make the code "neater" by putting everything inside the function, the code stops working. This is my original code that works :

static int readValidInt(Scanner in, String prompt, int min,  int max){
    while(!in.hasNextInt()) { //Makes sure that user inputs an Integer, not String, double, etc
        System.out.println("Sorry, only numbers in integer form is allowed. Please enter your choice as an integer between 1 and 4");
        in.next();
    }
    int a = in.nextInt();
    if ( a >= min && a <= max) {
        System.out.println("you have chosen board"+ a );
        return a;   
    }
    else {
        System.out.println(prompt);
        return 0;
    }
    //in main, use a do while loop to keep this running until the input is right(until a becomes something that is not 0)
}

public static void main (String args[]) {
    int validinput;
    Scanner input = new Scanner(System.in);
    System.out.println("WELCOME TO CS300 PEG SOLITAIR GAME !" + "\n====================================");
    System.out.println("Board Style Menu");
    System.out.println("\t1) Cross \n \t2) Circle \n \t3) Triangle \n\t3) Simple T"); //Prints out the 4 options of boards, spacing all of them with \t and \n
    System.out.println("Choose a board style");
    do {
        validinput = readValidInt(input, "Bruh that's not valid", 1, 4);
    }
    while (validinput == 0);
}

This is the output : https://i.stack.imgur.com/DpQEE.png

This is the code of the version where I tried to take the do while loop out of main:

public static int readValidInt(Scanner in, String prompt, int min,  int max){   
    int checker;
    int a;

    while(!in.hasNextInt()) { //Makes sure that user inputs an Integer, not String, double, etc
        System.out.println("Sorry, only numbers in integer form is allowed. Please enter your choice as an integer between 1 and 4");
        in.next();
    }
    do {
         a = in.nextInt();
            if ( a >= min && a <= max) {
                    System.out.println("you have chosen board"+ a );
                    checker = 1;
                    return a;   

                }
        else {
            System.out.println(prompt);
            checker = 0;
            return 0;
        }   
    }while (checker==0);
}

public static void main (String args[]) {
    
    Scanner input = new Scanner(System.in);
    System.out.println("WELCOME TO CS300 PEG SOLITssssAIR GAME !" + "\n====================================");
    System.out.println("Board Style Menu");
    System.out.println("\t1) Cross \n \t2) Circle \n \t3) Triangle \n\t4) Simple T"); //Prints out the 4 options of boards, spacing all of them with \t and \n
    System.out.println("Choose a board style");
    
    readValidInt(input, "Bruh that's not valid", 1, 4);
}

This is the output of the one that doesn't work (it terminates without giving me another attempt): https://i.stack.imgur.com/flOXj.png

Aucun commentaire:

Enregistrer un commentaire