Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Eclipse - Resource leak: input is never closed. How do i fix?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried a bunch of different ways and i in the text im about to paste, tried the "try" option and nothing i seem to do works. I cant get the message to go away on eclipse, and this damn input wont close! Here is what i have, can anyone find the problem?

import java.util.Scanner;

public class Project2 {

public static void main(String[] args) {
// TODO Auto-generated method stub
try (Scanner input = new Scanner(System.in)) {

// Annual interest rate
System.out
.print("Enter annual interest rate, for example 0.5, no percent sign:");
double annualInterestRate = input.nextDouble();

// Monthly interest rate
double monthlyInterestRate = annualInterestRate / 1200;

// Number of years
System.out.print("Enter number of years, for example 5: ");
int numberOfYears = input.nextInt();

// Investment amount
System.out
.print("Enter investment amount, for example 145000.95: ");
double loanAmount = input.nextDouble();

// Calculate payments
double monthlyPayment = loanAmount
* monthlyInterestRate
/ (1 - 1 / Math.pow(1 + monthlyInterestRate,
numberOfYears * 12));
double totalPayment = monthlyPayment * numberOfYears * 12;

System.out.println("The monthly payment is "
+ (int) (monthlyPayment * 100) / 100.0);

System.out.println("Accumulated value is "
+ (int) (totalPayment * 100) / 100.0);

}
}
}



 
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Devin,

Welcome to the Ranch!

Devin wesolowski wrote:I cant get the message to go away on eclipse, and this damn input wont close!


My advice is to ignore it. Eclipse treats all resources the same, while the System.in is a very special type of resource that once closed cannot be reopened. Eclipse is just making a recommendation based on it's limited understanding. You have a valid try-with-resource block. Actually, trying your code as posted in Eclipse doesn't generate a warning for me. So, perhaps you changed your settings under Java → Compiler → Errors/Warnings? Perhaps try changing those back to the Default.

Oh, and in the future please use code tags when posting code (← Please click here if you are not sure how). It makes your code much easier to read, allowing us to more effectively assist you.

Cheers!
Chris
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's how your code looks formatted and with CodeTags:



A lot better, huh?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
var.close();

You need to close scanner once it's used.
 
Saloon Keeper
Posts: 22637
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Deep Dd wrote:var.close();

You need to close scanner once it's used.



Go back and read what Chris said.

If that doesn't satisfy you, there's some good arguments about the topic between myself and Campbell Ritchie on this site if you search a little.
 
Marshal
Posts: 70610
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Deep Dd wrote:. . . You need to close scanner once it's used.

You need to close a Scanner reading anything except System.in. In this case, you shouldn't call the close() method because DW has written something better: try with resources.

I have seen that warning many times, and I take the following view of it:-
  • 1: I think it is an affliction peculiar to Eclipse, but I am not sure about that.
  • 2: Whoever programmed the Eclipse compiler managed to give it a better range of warnings and error messages than some other compilers.
  • 3: It is usually necessary to close all sorts of different resources, but Eclipse doesn't seem to identify System.in as the one exception which shouldn't be closed.
  • 4: I don't ignore that warning, but I disbelieve it. I don't use try with resources nor close().
  • 5: I hover my mouse over the code marked with the yellow wavy underlining until a drop‑down list of possible solutions appears.
  • 6: I choose the option to tag the declaration with @SuppressWarnings and goodbye warning message!
  • OP: please explain what you mean about input not closing. I can't see how the input would keep running for ever, unless you have managed to write while (input.hasNext()) ... or similar somewhere, which will produce an infinite loop. I tried your code and didn't experience any such problems.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic