jeudi 11 juin 2020

Using scanners in nested IF Statements - Java

I am working on some OOP in Java, but i can't get my scanners to work as required in the main class. Here is the snip of the code I am working with:

Scanner scanner = new Scanner(System.in);
    int choice = scanner.nextInt();
    boolean quit = false;

     if(choice == 1) {
     //TODO
            }

    else if (choice == 2) {

        while (!quit) {
            System.out.println("What is required to be modified" + "\n\t1. Teacher" + "\n\t2. Student");
            if(scanner.nextInt() == 1) {
                System.out.println("Select teacher to modify" + "\n\t1. Adam" + "\n\t2. John" + "\n\t3. Bob");
                if (scanner.nextInt() == 1) {
                    System.out.println("Select parameter to modify" + "\n\t1. Salary" + "\n\t2. Status");
                    if(scanner.nextInt() == 1) {
                        System.out.println("Current Salary is: " + Adam.getEmpSalary() + "\n\t 1. Change Salary ?");
                        if(scanner.nextInt() == 1) {
                            System.out.println ("Enter New Salary");
                            Adam.setEmpSalary(scanner.nextInt());
                        }
                    }
                    else if (scanner.nextInt() == 2) {
                        if (Adam.isEmpStatus()){
                            System.out.println("Current Teacher is: Active" + "\n\t 1. Change Status ?");
                            if(scanner.nextInt() == 1) {
                                Adam.setEmpStatus(false);
                            }else break;
                        }else if (!Adam.isEmpStatus()){
                            System.out.println("Current Salary is: Terminated" + "\n\t 1. Change Status ?");
                            if(scanner.nextInt() == 1) {
                                Adam.setEmpStatus(true);
                            }else break;
                        }

                    }

}else if (scanner.nextInt() == 2) {
                    System.out.println("Select parameter to modify" + "\n\t1. Salary" + "\n\t2. Status");
                    if(scanner.nextInt() == 1) {
                        System.out.println("Current Salary is: " + John.getEmpSalary() + "\n\t 1. Change Salary ?");
                        if(scanner.nextInt() == 1) {
                            System.out.println ("Enter New Salary");
                            John.setEmpSalary(scanner.nextInt());
                        }else break;
                    }
                    else if (scanner.nextInt() == 2) {
                        if (John.isEmpStatus()){
                            System.out.println("Current Salary is: Active" + "\n\t 1. Change Status ?");
                            if(scanner.nextInt() == 1) {
                                John.setEmpStatus(false);
                            }else break;
                        }else if (!John.isEmpStatus()){
                            System.out.println("Current Salary is: Terminated" + "\n\t 1. Change Status ?");
                            if(scanner.nextInt() == 1) {
                                John.setEmpStatus(true);
                            }else break;
                        }

                    }

Basically I introduced a scanner called choice where it will go throughout the code inputing choices from the user. The code will first ask a question and the user is required to enter either 1/2/3 etc.. based on the choice an if statement will be selected.

The code works well if my choices are only 1, while if I choose 2 or 3, in runtime the console waits for me to enter another value, i.e. if i select 2 at one of the choices, i have to input 2 again to be able to enter the required statement. I can't get my head around why is it acting that way.

Thanks !

Aucun commentaire:

Enregistrer un commentaire