[Solved] Java adding methods


The program you wrote have some bugs.

You are not assigning choice variable with the return value from readChoice() method.

choice = readChoice();

instead of readChoice();

Also change checkChoice() method as follows to make sure it shows message for all invalid choices like 0

public static int checkChoice(int choice) {
Scanner sc = new Scanner(System.in);
if (choice < 1 || choice > 4) {
        System.out.println("Invalid Input");

}else{
        return choice;
}
do {
    System.out.println("Enter your choice (1,2,3 or 4:): ");
    choice = sc.nextInt();
    if (choice < 1 || choice > 4) {
        System.out.println("Invalid Input");

    }
} while (choice < 1 || choice > 4);

return choice;

}

Assuming you want to ask the user to enter a choice until he enters a valid choice.

Also again reassign the value from checkChoice() to variable choice.

choice = checkChoice(choice);

2

solved Java adding methods