Results 1 to 2 of 2

Thread: help needed

  1. #1
    amarchandar is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default help needed

    thanks for reply,
    but i still have some errors.
    Im finding factors but i coudn't pass that factors in to a method to check whether it is a prime or not.
    here is my program
    import java.io.*;
    public class primefact2
    {
    public static void main(String[] args)throws Exception
    {
    int j,num,count=0;
    DataInputStream dis=new DataInputStream(System.in);
    System.out.println("enter number");
    num=Integer.parseInt(dis.readLine());
    for(int i=1;i<num;i++)
    {
    if(num%i==0)
    {
    System.out.println(i);

    }
    for(i=1;i<num;i++)
    {
    // primefact2.isPrime(int i)
    if(primefact2.isPrime(i))
    {
    count=count+1;
    }
    }
    System.out.println(count);
    }
    }
    static int isPrime(int n)
    {
    for(int i=2;i<n;i++)
    {
    if(n%i==0)
    {
    return 0;
    }
    }
    return i;
    }
    }

  2. #2
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    your main problem is in your method :

    Java Code:
    static int isPrime(int n)
    {
       for(int i=2;i<n;i++)
            if(n%i==0) return 0;
    
       return i;
    }
    1) you can't read the variable i outside of the for statement because is definded and locally visible inside the for statement
    2) if the code you call

    Java Code:
    if(primefact2.isPrime(i))   count=count+1;
    this supposes the return type of the method isPrime is a boolean so you should rewrite your method like this and then there shouldn't be any problems:

    Java Code:
    static boolean isPrime(int n)
    {
       for(int i=2;i<n;i++)
            if(n%i==0) return false;
    
       return true;
    }

Similar Threads

  1. Help needed
    By harshalsakpal in forum CLDC and MIDP
    Replies: 0
    Last Post: 02-17-2009, 03:53 PM
  2. help needed!!! :S
    By mark-mlt in forum Networking
    Replies: 1
    Last Post: 04-14-2008, 09:27 AM
  3. Help needed.
    By necro-1000 in forum AWT / Swing
    Replies: 2
    Last Post: 01-12-2008, 11:37 AM
  4. Help Needed
    By pks in forum New To Java
    Replies: 1
    Last Post: 01-09-2008, 12:07 AM
  5. help needed.
    By dirtycash in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 09:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •