Pergunta

It seems that when I create my scanner I get this error. I have tried to solve this by searching the error name, but have so far been unsuccessful in getting the message to stop appearing.

Code:

import java.util.Scanner;
public class PrintQueue {
    //Instance variables
    private Queue<Job> pq;
    //Constructor
    public PrintQueue() {
        pq = new Queue<Job>();
    }
    //Adds a job object to the end of the queue
    public void lpr(String owner, int jobId) {
        Job j = new Job(owner, jobId);
        pq.enqueue(j);
    }
    //Enumerates the queue
    public void lpq() {
        Job curr = pq.first();
        for (int i = 0; i < pq.size(); i++) {
            System.out.println(curr);
            curr = pq.next();
        }
    }
    //Removes the first entry in the queue if the input integer matches the integer contained within the job object
    public void lprm(int jobId) {
        if (pq.first().getJobId() == (jobId))
            pq.dequeue();
        else 
            System.out.println("Unable to find jobId.");
    }
    //Removes all objects that contain the input String
    public void lprmAll(String owner) {
        Job curr = pq.first();
        for (int i = 0; i < pq.size(); i++) {
            if (curr.getOwner().equals(owner)) 
                pq.dequeue();
            curr = pq.next();
        }
    }
    //Demo 
    public static void main(String[] args) {
        Scanner k = new Scanner(System.in);
        PrintQueue myPQ = new PrintQueue();
        String name;
        int id;
        for (int i = 1; i <= 5; i++) {
            System.out.print("Enter owner and id: ");
            name = k.next();
            id = k.nextInt();
            myPQ.lpr(name, id);
        }
        System.out.println("Print Queue");
        myPQ.lpq();
        myPQ.lprm(101);
        myPQ.lprmAll("ronaldinho");
        System.out.println("Print Queue"); 
        System.out.println("\n\n"); 
        myPQ.lpq(); 
    }
}

Part where I get the error:

Scanner k = new Scanner(System.in);
Foi útil?

Solução

That's because you're never closing the Scanner. Change your code to:

Scanner k = null;
try {
    k = new Scanner(System.in);
    //do stuff with k here...
} finally {
    if( k != null )
        k.close();
}

Outras dicas

It seems that it is rather warning than error. However it is good practice to solve it.

Actually you just have to call k.close(); in the end of your method. The best practice is to call close in finally block: this guarantees that the resource is closed whenever exception is thrown or not;

Scanner k = null;
try {
    k = new Scanner(System.in);
    ........
} finally {
    if (k != null) {
        k.close();
    }
}

Fortunately java 7 provides makes this syntax less verbose:

try (
    Scanner k = new Scanner(System.in);
) {
    .... // use k
} 

When object of any class that implements Closable is created in special section of try block that marked with regular brackets () you do not have to write finally block: it is added by compiler.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top