2016-12-31 61 views
-1
package ticketBook; 
import java.util.*; 

public class run { 

    public static void main(String args[]){ 
     **Scanner in = new Scanner(System.in)**; "Resource leak: 'in' is never closed" 
     int ch,i,n,movch; String rowch[]=new String[12]; 
     int colch[]=new int[12]; 
     String time = new String(); 
     String timecheck = new String(); 
     show objshow = new show(); 
     screen objscreen = new screen(); 
     ticket objticket = new ticket(); 
     while(true){ 
     System.out.println("\n\t\t Movie Ticket Booking System\n"); 
     System.out.println("1.Book Movie Tickets"); 
     System.out.println("2.See Movies/Show Timings"); 
     System.out.println("3.Exit"); 
     System.out.println("\nAdvanced Bookings are closed as of now."); 
     System.out.println("\nEnter Choice"); 
     ch = in.nextInt(); 
     switch(ch) 
     { 
      case 1: objshow.display_show_timings(); 
        System.out.println("Choose the Movie"); 
        movch=in.nextInt(); 
        do{ 
        System.out.println("Enter Show timings (hh:mm)"); 
        time=in.next(); 
        timecheck=objshow.check_time(movch,time); 
        }while(timecheck.equals("false")); 
        System.out.println("Enter Number of seats"); 
        n=in.nextInt(); 
        for(i=0;i<n;i++){ 
         objscreen.display_seats(); 
         rowch[i]=in.next().toUpperCase(); 
         objshow.show_empty_seats(rowch[i],movch); 
         colch[i]=in.nextInt(); 
         if(objshow.check_seats(rowch[i],colch[i],movch)) 
         { 
          System.out.println("Seat is not available"); 
         } 
         objshow.fill_seat(rowch[i],colch[i],movch); 
        } 
        for(i=0;i<n;i++) 
        { 
           objticket.print_ticket(objshow.getmovie(movch), time, 3, objscreen.getcat(rowch[i]), rowch[i], colch[i]); 
        } 
        break; 

      case 2: objshow.display_show_timings(); 
        break; 

      case 3: System.exit(0);   
     } 
    } 
    } 
} 

虽然出口到它错误结束可执行的JAR文件:Java编程在Eclipse出口错误

Resource leak: 'in' is never closed.

+0

我想这是不是一个错误。这只是一个警告。 – Jens

回答

1

你想用try with resources,这将在try结束时自动关闭流。

public static void main(String args[]){ 
     try(Scanner in = new Scanner(System.in)) { 
      // rest of the code. 
     } catch (Exception e){ 
      e.printStackTrace(); 
     } 
    } 

如果您使用JDK6或之前:

public static void main(String args[]){ 
     Scanner in = new Scanner(System.in); 
     try { 
      // rest of the code. 
     } catch (Exception e){ 
      e.printStackTrace(); 
     } finally { 
      in.close(); 
     } 
    }