简体   繁体   中英

Scanner in a function. Closing the Scanner is causing trouble

I am getting flickering screen when closing the scanner,but without closing it works fine.

public void removeBranch() {
        try {
            Scanner input=new Scanner(System.in);
            System.out.print("Enter branch id to remove:");
            int Id=input.nextInt();
            int toDelete=branchPresent(Id);
            if(toDelete!=-1) {
                branches.remove(toDelete);
                System.out.println("Branch removed");
            }else {
                System.out.println("\n No such Branch!\n");
            }
        } catch (Exception e) {
            System.out.println("\nsomething went wrong while removing  !\n");
        }

    }

Close the scanner in finally block

try{
Scanner input=new Scanner(System.in);
// Do stuff
}
catch {
// Handle exception
}
finally {
input.close();
}


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM