Why is there an error message? – resource leak “scan” is never closed
Line 12: Scanner scan = new Scanner (System.in);
package LP2PAT3ChallengeExercise;
import java.util.Scanner;
public class LoveCS {
public static void main(String[] args)
{
int limit;
Scanner scan = new Scanner (System.in);
limit=Integer.parseInt(scan.nextLine())
;
// ASK THE USER HOW MANY TIMES THE MESSAGE SHOULD BE PRINTED
// STORE THE USER’S RESPONSE IN THE limit VARIABLE
int sum = 0;
int count = 1;
int line_no=1;
while(limit>=1) {
System.out.println(line_no+ “I love Computer Science”);
count=count+1;
sum=sum+count;
limit–;
line_no++;
}
System.out.println ( “Printed this message ” +count+” times”);
System.out.println(“The sum of the number from 1 to “+count+ “is” +sum);
// CREATE A WHILE LOOP THAT WILL LOOP limit TIMES, PRINTING THE LINE
// NUMBER FOLLOWED BY “I love Computer Science”
// PRINT TO CONSOLE “Printed this message X times” WITH X = THE NUMBER OF
// TIMES THE LOOP WAS PERFORMED
// PRINT TO CONSOLE “The sum of the number from 1 to X is Y” WITH X = THE
// NUMBER OF TIMES THE LOOP WAS PERFORMED AND Y = THE TOTAL OF
// THE LINE NUMBERS
}
}