Sauhard Chand
Sauhard Chand

Reputation: 1

How to Use Scanner close in this Prorgram?

import java.util.Scanner;                      //Scanner is imported here
public class Reverse {

    public static void main(String[] args) {
    int rev=0, rem;
    Scanner s = new Scanner(System.in);        //Scanner is used here and error is shown in this line
    System.out.println("Enter a number ");
    int a=s.nextInt();
    int b=a;
    
    while(b!=0)
    {
    rem=b%10;
    rev=rev*10+rem;
    b=b/10;
    }
    
    System.out.println("The reverse of the number is " +rev);
    
    if(a==rev)
    {
        System.out.println("The number is Palindrome.");
    }
    else
    {
        System.out.println("The number is not Palindrome");
    }
}
}

Upvotes: 0

Views: 31

Answers (1)

Pratyay Biswas
Pratyay Biswas

Reputation: 106

use s.close(); at the end within main function.

Your program will look like this:-

public static void main(String[] args) {
int rev=0, rem;
Scanner s = new Scanner(System.in);        //Scanner is used here and error is shown in this line
System.out.println("Enter a number ");
int a=s.nextInt();
int b=a;

while(b!=0)
{
rem=b%10;
rev=rev*10+rem;
b=b/10;
}

System.out.println("The reverse of the number is " +rev);

if(a==rev)
{
    System.out.println("The number is Palindrome.");
}
else
{
    System.out.println("The number is not Palindrome");
}

s.close();                        //SCANNER ENDS HERE.

Upvotes: 1

Related Questions