Home > Net >  Why my boolean is unreachable code and compilation error?
Why my boolean is unreachable code and compilation error?

Time:01-06

I am doing a leave management system for my assignment. And this is the part where I ask user to save their registration. First, I use do...while(true) loop to ask for their info like name, ID , department, etc. I start with adding a boolean regErr = false before the do...while(true) loop and it has no problem.

Then I continue with this part and add a boolean saveReg. But I have no idea why it shows error and says its unreachable code. If I add the boolean saveReg inside do...while loop also doesn't work. I really need your help to figure out what's the problem. Thank you in advance and have a nice day!

//the do...while loop for their name, ID, etc. where it has no problem //I simply write down here in case u want to have a look

                   boolean regErr = false;

                   do{
                      regErr = false;

                      System.out.print("Name: ");
                      :
                      :

                   }while(true)

//and here is the part where it has problem

                    boolean saveReg;    //the boolean that is unreachable code
        
        do{
            String save = "";
            System.out.print("Save account?(Y/N):  ");
            save = sc.next();
        
            if(save.equals("Y") || save.equals("y")){
                saveReg = true;
                break;
            }
            else if(save.equals("N") || save.equals("n")){
                saveReg = false;
                break;
            }
            else{
                System.out.println("Invalid choice.");
            }
        }while(true);
    
    if(saveReg){
        try(PrintWriter regWriter = new PrintWriter("./empData/"   ".txt", "UTF-8")){
            regWriter.println(name);
            regWriter.println(empID);
            regWriter.println(password);
            regWriter.println(department);
            regWriter.println(position);
            regWriter.println(dateJoined);
            regWriter.println(userRole);
            regWriter.println(superiorID);
            
            System.out.println("Account saved.");
            regWriter.close();
            
            try(PrintWriter updateRegNo = new PrintWriter("./RegNo.txt", "UTF-8")){
                updateRegNo.println(  regNo);
                updateRegNo.close();
            }catch(IOException err){
                err.printStackTrace();
            }
        }catch(IOException err){
            err.printStackTrace();
        }
    }
    else{
        System.out.println("Registration fail.");
    }

CodePudding user response:

That is because of your first code, while(true) will make it run for infinite times so code below this is unreachable. Use below code -

 boolean regErr = false;

 do{
     regErr = false;

     System.out.print("Name: ");
                  :
                  :

  }while(regErr )
  • Related