#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    42
    Rep Power
    7

    Scanner resource leak


    I am currently working on a program and I am working with Eclipse as my IDE.

    Initially, my code working without any issue. And then, someone used it, and ever since then, I have been getting some issues.

    The problem is related to my Scanner object declaration. Everything that I create the Scanner object, a yellow triangle with an exclamation mark inside appears on the left. The error message is the following:

    "Resource leak: input is never closed."

    And here is my current code.

    Code:
    public class PatrickTchakounteNjomgangHw2 {
    	public static void main(String[] args) {
    		//Print author's information
    		System.out.println("Patrick Tchakounte Njomgang, Java Homework 2, Tues/Thur 9:30 am");
    		
    		//Create a Scanner object
    		Scanner input = new Scanner(System.in);
    		
    		//Receive input
    		System.out.println("Enter hourly pay");
    		double hourlyPay = input.nextDouble();
    		
    		System.out.println("Enter hours worked");
    		int hoursWorked = input.nextInt();
    		
    		System.out.println("Enter tax rate");
    		double taxRate = input.nextDouble()/100;
    		
    		//Calculate total income before tax, total income after tax, actual tax
    		double grossIncome = hourlyPay * hoursWorked;
    		double netIncome = grossIncome - (grossIncome * (taxRate));
    		double taxAmount = grossIncome * taxRate;
    		
    		//Print outputs
    		System.out.println("The total income before tax is $" + (grossIncome));
    		System.out.println("The total income after tax is $" + (netIncome));
    		System.out.println("The actual tax is $" + ((int)(taxAmount * 100)/100.0));
    	}
    }
    As I stated earlier, I have not changed the code, and there were no errors before.

    I tried the following. This code is only for demonstration purposes.

    Code:
    Scanner input = new Scanner(System.in);  
    try  
    {  
       //all code which uses the scanner  
    }  
    finally  
    {  
        input.close();  //In finally to make sure it always gets closed  
    }
    I still get the triangle next to the line number, but it is only greyed out. But the yellow error still shows up on my Project Explorer next to my source file name.

    So if someone could help me I would appreciate.
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,040
    Rep Power
    1291
    I just tried what you suggested, and I didn't get a warning.

    Code:
    import java.util.Scanner;
    
    public class PatrickTchakounteNjomgangHw2 {
    	public static void main(String[] args) {
    		//Print author's information
    		System.out.println("Patrick Tchakounte Njomgang, Java Homework 2, Tues/Thur 9:30 am");
    		
    		//Create a Scanner object
    		Scanner input = new Scanner(System.in);
    		
    		try  
    		{  
    		   //all code which uses the scanner  
    		}  
    		finally  
    		{  
    		    input.close();  //In finally to make sure it always gets closed  
    		}
    		
    		//Receive input
    		System.out.println("Enter hourly pay");
    		double hourlyPay = input.nextDouble();
    		
    		System.out.println("Enter hours worked");
    		int hoursWorked = input.nextInt();
    		
    		System.out.println("Enter tax rate");
    		double taxRate = input.nextDouble()/100;
    		
    		//Calculate total income before tax, total income after tax, actual tax
    		double grossIncome = hourlyPay * hoursWorked;
    		double netIncome = grossIncome - (grossIncome * (taxRate));
    		double taxAmount = grossIncome * taxRate;
    		
    		//Print outputs
    		System.out.println("The total income before tax is $" + (grossIncome));
    		System.out.println("The total income after tax is $" + (netIncome));
    		System.out.println("The actual tax is $" + ((int)(taxAmount * 100)/100.0));
    	}
    }

IMN logo majestic logo threadwatch logo seochat tools logo