3 Replies - 872 Views - Last Post: 21 February 2014 - 04:19 PM Rate Topic: -----

#1 brittishsnow   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 11-October 13

I have a Resource leak error and I am not sure how to fix it.

Posted 21 February 2014 - 04:10 PM

I have a Resource leak error and I am not sure how to fix it. The error is "Resource leak: 'numberinput' is never closed"

the section of code that has this error is
Scanner numberinput = new Scanner(System.in);  
String choice = numberinput.nextLine();



If you require the rest of the code to help me here it is

/**
 * @author Maxwell Stow
 * date: February 21, 2014
 * assignment name: Exercise 2
 */
import java.util.*;
public class DigitExtractor
{
    public static void main (String args[])
    {
        //This code should be placed inside the main method of a class
    	System.out.println("Enter an number greater than 100: ");
    	Scanner number = new Scanner(System.in);
    	int num = number.nextInt();
        loop: while (num > 99){
        System.out.println("show (W)hole number.");
        System.out.println("show (O)nes place digit.");
        System.out.println("show (T)ens place digit.");
        System.out.println("show (H)undreds place digit.");
        System.out.println("(Q)uit\n");
        System.out.println("Enter your choice: ");
 		Scanner numberinput = new Scanner(System.in);  
        String choice = numberinput.nextLine();
        char input = choice.charAt(0);	
        switch(input)
                {
            case 'W': //Whole number
            case 'w': //Notice we are providing for both capital W and little w.
                System.out.println("The whole number is: " + num);
                break;
            case 'O': //Ones digit
            case 'o':
            	System.out.println("The Ones place digit is: " + (num % 10));
                break;
            case 'T': //Tens digit
            case 't':
            	System.out.println("The Tens place digit is: " + (num / 10 % 10));
                break;
            case 'H': //hundreds digit
            case 'h':
            	System.out.println("The Hundreds place is: " + (num /100 % 10));
                break;
            case 'Q': //Quit
            case 'q': 
            	System.out.println("Good Bye");
            	System.exit(input);
            	break loop;
            default:
                System.out.println("Please enter either a W, O, T, H, or Q, other inputs will not be discarded. Thank you.");
                break;
                }       
        }
}
}



Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: I have a Resource leak error and I am not sure how to fix it.

#2 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6563
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Re: I have a Resource leak error and I am not sure how to fix it.

Posted 21 February 2014 - 04:15 PM

You only need, and should only have, one Scanner for System.in.
Was This Post Helpful? 0
  • +
  • -

#3 brittishsnow   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 11-October 13

Re: I have a Resource leak error and I am not sure how to fix it.

Posted 21 February 2014 - 04:18 PM

View Postandrewsw, on 21 February 2014 - 04:15 PM, said:

You only need, and should only have, one Scanner for System.in.

Alright well I just changed it to this

Scanner numberinput = new Scanner(System());  
String choice = numberinput.nextLine();



And it still has the error
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1577
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: I have a Resource leak error and I am not sure how to fix it.

Posted 21 February 2014 - 04:19 PM

It isn't an error, it is just a warning
You wouldn't close the System.in stream anyway, just leave it open. Use just one Scanner as andrewsw pointed out
I don't like your use of goto....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1