package findbook; import java.util.InputMismatchException; import java.util.Scanner; public class Find { public static void main(String[] args) throws Nobook { // TODO 自动生成的方法存根 String[] book= {"高数","英语","JAVA","信号"}; Find book1=new Find(); book1.findways(book); } //查找方法 public void findways(String[] book) throws Nobook{ Scanner in=new Scanner(System.in); try { System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书"); int a=in.nextInt(); switch(a) { case 1: findname(book); break; case 2: findnumber(book); break; } in.close(); }catch(InputMismatchException e) { System.out.println("请输入整数!!!"); findways(book); } } //按书名查找 public void findname(String[] book) throws Nobook{ Scanner in=new Scanner(System.in); System.out.println("输入图书名称:"); String a=in.nextLine(); boolean flag=false; try { for(int i=0;i<book.length;i++) { if(a.equals(book[i])) { System.out.println("book:"+book[i]); flag=true; break; } } in.close(); if(!flag) { throw new Nobook(); } }catch(Nobook e){ findways(book); } } //按序号查找 public void findnumber(String[] book) throws Nobook{ Scanner in=new Scanner(System.in); System.out.println("输入图书序号:"); int a=in.nextInt(); boolean flag=false; try { for(int i=0;i<book.length;i++) { if(a==i) { System.out.println("book:"+book[i]); flag=true; break; } } in.close(); if(!flag) { throw new Nobook(); } }catch(InputMismatchException e) { System.out.println("请输入整数!!!"); findways(book); }catch(Nobook e) { System.out.println(e.getMessage()); findways(book); } } } //自定义异常 package findbook; public class Nobook extends Exception{ public Nobook(){ System.out.println("图书不存在!"); } }
Scanner作为扫描对象的释放速度不够快,远远比不上你的程序运行速度,所以你在程序里面多次创建和关闭Scanner,造成资源还没有来得及释放的时候,程序就运行到该处了。这种情况,你可以将Scanner在main()中创建对象,将引用作为参数给各个函数用,然后在main()最后一同释放。