This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

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: 6259
167
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?
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!