Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Resource leak: 'in' is never closed..?

  1. #1
    Junior Member missm's Avatar
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Resource leak: 'in' is never closed..?

    Hello everyone! I need help with two things:
    1. I get this warning "Resource leak: 'in' is never closed". Why?
    2. After every convert is done I want the following message to be printed(example): "1500 miles equals to 2414.02 kilometers". How do I do that?
    Thank you
    import java.util.Scanner;
     
    public class Convert {
     
    	public static void main(String[] args) {
     
    	Scanner in = new Scanner (System.in);
     
    	float miles;
    	float feet;
    	float galons;
    	float km1, km2;
    	float meters1, meters2;
    	float liters1, liters2;
     
    	System.out.println("Miles:");
    	miles = in.nextFloat();
    	km1 = (float) (miles * 1.609344);
    	km2 = (float) (Math.round(km1*100.0)/100.0);	
    	System.out.println("Kilometers ="+km2);
     
    	System.out.println("Feet:");
    	feet = in.nextFloat();
    	meters1 = (float) (feet * 0.3048);
    	meters2 = (float) (Math.round(meters1*100.0)/100.0);	
    	System.out.println("Meters = "+meters2);
     
    	System.out.println("Galons:");
    	galons = in.nextFloat();
    	liters1 = (float) (galons * 3.78541178);
    	liters2 = (float) (Math.round(liters1*100.0)/100.0);	
    	System.out.println("Liters "+liters2);
    	}
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Resource leak: 'in' is never closed..?

    Quote Originally Posted by missm View Post
    Hello everyone! I need help with two things:
    1. I get this warning "Resource leak: 'in' is never closed". Why?
    You're using a Scanner object but not closing. At the bottom of your program, close the Scanner via in.close();

    2. After every convert is done I want the following message to be printed(example): "1500 miles equals to 2414.02 kilometers". How do I do that?
    You can use variables with your System.out.println statements -- shoot, you're already doing that. So what have you tried and what isn't working with your attempt to do this?

  3. #3
    Junior Member missm's Avatar
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Resource leak: 'in' is never closed..?

    I can't really say I have been trying something except for reading tutorials and the book I have (im referring to number 2. Today I had my first class in Java and this is homework, as you can understand).
    However thank you. I did something now and it works. Thank you again!
    Last edited by missm; October 3rd, 2012 at 02:13 PM.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Resource leak: 'in' is never closed..?

    You're welcome!

Similar Threads

  1. The requested resource () is not available.
    By freakycoder in forum Web Frameworks
    Replies: 0
    Last Post: July 10th, 2012, 01:44 AM
  2. Might be a thread leak problem reading a file into an Array list.
    By rushtonjj in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 10th, 2012, 07:56 AM
  3. could anybody fix my problem? resultset is closed
    By kagami in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 18th, 2012, 09:18 AM
  4. Result set is closed
    By keshav_agrawal89 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 11th, 2011, 09:35 AM
  5. ResultSet is Closed
    By ramayya4u in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: May 24th, 2009, 04:54 AM