-3

I have to write a code using functions where the user inputs a number and then a function verifies if that number it's prime or not. I'm having trouble making the connection between my main function and my "prime" function.

public static void main(String args []){
    Scanner input = new Scanner(System.in);
    int number;
    int i;
    int a;

    System.out.println("Enter number");
    number = input.nextInt();

    for(i = 1; i < (number + 1); i++){
        if(number % i==0){
            a++;
        }
    }

    System.out.println(prime(m));
}

public static boolean prime(boolean m){
    boolean m = false;

    if(a!=2){
        return m;
    }else{
        return m = true;
    } 
}
takendarkk
  • 3,347
  • 8
  • 25
  • 37
PBRD
  • 67
  • 3
  • 10
  • Well, I don't know why I get a message that "m" has not been initialized. – PBRD May 17 '15 at 01:25
  • `m` shouldn't even be a String but rather a boolean. Initialize it to something, say false, and then change it as needed. – Hovercraft Full Of Eels May 17 '15 at 01:27
  • m = false; you mean? – PBRD May 17 '15 at 01:30
  • It is pretty simple. Collect some prime numbers in a list and then ask a list if it contains a given number. – MaxZoom May 17 '15 at 01:31
  • I tried with boolean but I keep getting the same message that "m" cannot be found. – PBRD May 17 '15 at 01:48
  • `System.out.println(prime(m));` Do you think m has been initialized when this line executes? – takendarkk May 17 '15 at 01:50
  • I'm triying to call the function that returns "m" – PBRD May 17 '15 at 01:51
  • Your prime method should be taking in a number (`int`), not a boolean. Also, your prime method doesn't need a boolean variable at all. Just return true or return false. Also, `a` doesn't exist in the scope of the prime method. You declared it inside of main. Also, your prime method is poorly named because it is not checking if a number is prime or not, it just checks if a number is 2 or not. You prime number logic is inside main. – takendarkk May 17 '15 at 01:55
  • 1
    You can check up to the square root of a number to make the algorithm faster. – William Falcon May 17 '15 at 01:57

3 Answers3

0

From my understanding of your code, you ask the user to input a number and then check all the numbers that divide it from 1 to n. If the amount of numbers that divide it doesn't equal two then the number is prime.

This means that the prime function should take an integer as its argument, this integer in your case is the variable a.

The next thing you want is the the prime function to return true or false.

Given these observations here is the code below:

public class IsPrime {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    int number, i, a = 0;

    System.out.println("Enter a number: ");
    number = in.nextInt();

    for (i = 1; i < (number + 1); i++) {
        if (number % i == 0) {
            a++;
        }
    }

    System.out.println(prime(a));
}

public static boolean prime(int a) {
    if (a != 2) {
        return false;
    }

    return true;
}

}
user2525951
  • 30
  • 1
  • 7
  • This code works. It seems all I had to do was to declare "prime" as a boolean and use an integer "a". – PBRD May 17 '15 at 02:02
0

You get the message "m" has not been initialized because Java expects you to initialize local variables before you use them (and there are good reasons for that as you can read here and also here

Also you should return a boolean instead of a String. Change your prime method to:

public static boolean prime(int number){

    int i;

    // a should not be a parameter but a local variable
    int a = 0;

    for(i=1;i<(number + 1);i++){
        if(number % i==0){
            a++;
        }
    }

    // if the number of dividers is 2 (only 1 and the number itself), then its a prime number, so a==2 will give you true as result for a prime number
    return (a == 2);

}

Community
  • 1
  • 1
alainlompo
  • 4,414
  • 4
  • 32
  • 41
0

This is a very strange way to check for primality. You don't need to count all the factors: you only need to find one of them. Try this:

public static void main(String args []){
    Scanner input = new Scanner(System.in);

    System.out.println("Enter number");
    int number = input.nextInt();

    System.out.println(isPrime(number));
}

public static boolean isPrime(int number){

    for (int i = 3; i*i <= number; i++)
    {
        if (number % i != 0)
            return false;
    }
    return true;
}
user207421
  • 305,947
  • 44
  • 307
  • 483