Click here to Skip to main content
14,301,556 members
Rate this:
Please Sign up or sign in to vote.
See more:
it is a smith number program and i am getting error in void accept ureported exception in java.io must be caught or declared thrown

What I have tried:

import java.io.*;
class smith
{
    int num;int sum;
    smith()
    {
        num=0;
        sum=0;
    }
    void accept ()throws IOException
    {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    num=Integer.parseInt(in.readLine());
}
int sumdigit(int x)
{
    int s=0;
    while(x!=0)
    {
        int d=x%10;
        s=s+d;
        x=x%10;
    }
    return s;
}
void check()
{
    int r=sumdigit(num);
    int i=2;
    while(num>1)
    {
        if (num%i==0)
        {
            sum=sum+sumdigit(i);
            num=num/i;
        }
        else
        {
            i++;
        }
    }
    if(sum==r)
    System.out.println("number is a smith number");
    else
    System.out.println("number is not a smith number");
}
void main()
{
    smith obj=new smith();
    obj.accept ();
    obj.check();
}
}
Posted
Updated 9-Sep-19 6:11am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

num=Integer.parseInt(in.readLine()); // this can throw an exception, see below.

Integer (Java Platform SE 8 )[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100